Hi,
I made a script that opens the source of the frontmost Safari document in TextMate, with syntax-coloring. I used a similar one for BBEdit a long time ago. (or was it only 5 months? ;)
With Applescript, it always look quite easy when you start, then you end up spending hours to get it to work properly. AS is as easy to read as it's hard to write. But it works! You can see I'm a little AS challenged, so advices are welcome.
Basically, it takes the source and the title of the frontmost Safari window (warns you if there is none, or if it's blank), remove "http://" and replace any "/" in the title with ":", makes a file in /tmp with the title as name (adding ".html" if it's not already there) and opens it in TM.
Should I put it on the wiki or in the repository?
Hope this can be useful to someone.
-- Fred
Download it here: http://osxgeek.org/tm/Source2TM.zip or copy/paste it Script Editor.
======================================================================== = tell application "Safari" if not (exists document 1) then display dialog "You need to open a web location first!" buttons {"OK"} default button 1 return end if set mySource to the source of front document as text if (length of mySource is 0) then display dialog "You need to open a web location first!" buttons {"OK"} default button 1 return end if set myName to name of front document as text end tell
try tell application "TextMate" to activate set myName to replace_chars(myName, "http://", "") set myName to replace_chars(myName, "/", ":") if (myName ends with ".html") or (myName ends with ".htm") then set myPath to "/tmp/" & myName else set myPath to "/tmp/" & myName & ".html" end if do shell script "rm -f " & quoted form of myPath do shell script "echo " & quoted form of mySource & " >> " & quoted form of myPath do shell script "open -a TextMate " & quoted form of myPath end try
on replace_chars(this_text, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replace_chars ======================================================================== =
Op 3-mrt-05 om 19:33 heeft Fred B. het volgende geschreven:
I made a script that opens the source of the frontmost Safari document in TextMate, with syntax-coloring.
That's cool. Would it also work for FireFox? Another cool thing which BBEdit has is you can press Cmd-R in your document and it asks you if you want to refresh the frontmost window of your browser. Handy when you're editing a file over sftp.
I'm Applescript illiterate, so maybe …
On Mar 3, 2005, at 19:40, Nednieuws wrote:
I made a script that opens the source of the frontmost Safari document in TextMate, with syntax-coloring.
That's cool. Would it also work for FireFox? Another cool thing which BBEdit has is you can press Cmd-R in your document and it asks you if you want to refresh the frontmost window of your browser. Handy when you're editing a file over sftp.
TextMate already has such a command for Safari, although it just reloads, it doesn't ask. I just extended the command to favor OmniWeb, if that browser is already running.
Op 3-mrt-05 om 20:09 heeft Allan Odgaard het volgende geschreven:
That's cool. Would it also work for FireFox? Another cool thing which BBEdit has is you can press Cmd-R in your document and it asks you if you want to refresh the frontmost window of your browser. Handy when you're editing a file over sftp.
TextMate already has such a command for Safari, although it just reloads, it doesn't ask. I just extended the command to favor OmniWeb, if that browser is already running.
I envision a configuration option for browser preference (maybe primary and secondary) and Cmd-R/Cmd-Opt-R :).
Why not Firefox? The web developer's browser of choice? (Flame on!)
On Mar 3, 2005, at 20:19, Nednieuws wrote:
TextMate already has such a command for Safari, although it just reloads, it doesn't ask. I just extended the command to favor OmniWeb, if that browser is already running.
I envision a configuration option for browser preference (maybe primary and secondary) and Cmd-R/Cmd-Opt-R :).
Well, having it check which of the browsers are running and just use the one which is, is probably desired in 99% of the cases (I have an aversion toward different hot keys and settings).
I guess the script could be smarter and detect when multiple browsers are running. present a dialog where the user could choose, and even make that choice sticky (see the recent get_doc_path.sh script I added to the repository for an example of how to have bundle items manage settings).
Why not Firefox? The web developer's browser of choice? (Flame on!)
Anyone can add to the command I committed (I even invited people to do that in the log ;) ). Personally I'm not a web developer, so I do not have Firefox. OmniWeb is my browser of choice, and I love it! :)
********************************************************************* Quoting Allan Odgaard on [Thursday, March 3, 2005, +0100]
I made a script that opens the source of the frontmost Safari document in TextMate, with syntax-coloring.
That's cool. Would it also work for FireFox? Another cool thing which BBEdit has is you can press Cmd-R in your document and it asks you if you want to refresh the frontmost window of your browser. Handy when you're editing a file over sftp.
TextMate already has such a command for Safari, although it just reloads, it doesn't ask. I just extended the command to favor OmniWeb, if that browser is already running.
As an Omniweb user, thanks Allan.
Cheers, Robert
I'm a bit new to the OS X platform (already i can't do without textmate!) but shouldn't it be possible to detect the default browser and refresh that. After i picked a different default browser, Firefox in my case, some apps seem to notice and open firefox instead of safari when a browser is needed (netnewswire for instance).
Tijs
On Thu, 3 Mar 2005 20:25:46 +0100, Allan Odgaard allan@macromates.com wrote:
On Mar 3, 2005, at 20:19, Nednieuws wrote:
TextMate already has such a command for Safari, although it just reloads, it doesn't ask. I just extended the command to favor OmniWeb, if that browser is already running.
I envision a configuration option for browser preference (maybe primary and secondary) and Cmd-R/Cmd-Opt-R :).
Well, having it check which of the browsers are running and just use the one which is, is probably desired in 99% of the cases (I have an aversion toward different hot keys and settings).
I guess the script could be smarter and detect when multiple browsers are running. present a dialog where the user could choose, and even make that choice sticky (see the recent get_doc_path.sh script I added to the repository for an example of how to have bundle items manage settings).
Why not Firefox? The web developer's browser of choice? (Flame on!)
Anyone can add to the command I committed (I even invited people to do that in the log ;) ). Personally I'm not a web developer, so I do not have Firefox. OmniWeb is my browser of choice, and I love it! :)
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
I played around a while ago and came up with these:
http://www.helvector.org/code/applescript/
Essentially they map the dot.local domain to the file system then open the file, or project in TextMate.
I never quite got round finishing them properly but they might come in useful for anyone who's web developing and using using their local machine for testing (if not they'll probably work but with more hacking). The only issue I have is having to use the script menu to execute them. Unfortunately I couldn't get them to work using OnMyCommand - for some reason sub functions didn't fire and I haven't had chance to look into why.
If you want syntax colouring via the Safari source window then I'd recommend SafariSource which you'll find on versiontracker.
Simon
On 3 Mar 2005, at 6:33 pm, Fred B. wrote:
Hi,
I made a script that opens the source of the frontmost Safari document in TextMate, with syntax-coloring. I used a similar one for BBEdit a long time ago. (or was it only 5 months? ;)
With Applescript, it always look quite easy when you start, then you end up spending hours to get it to work properly. AS is as easy to read as it's hard to write. But it works! You can see I'm a little AS challenged, so advices are welcome.
Basically, it takes the source and the title of the frontmost Safari window (warns you if there is none, or if it's blank), remove "http://" and replace any "/" in the title with ":", makes a file in /tmp with the title as name (adding ".html" if it's not already there) and opens it in TM.
Should I put it on the wiki or in the repository?
Hope this can be useful to someone.
-- Fred
Download it here: http://osxgeek.org/tm/Source2TM.zip or copy/paste it Script Editor.
=======================================================================
tell application "Safari" if not (exists document 1) then display dialog "You need to open a web location first!" buttons {"OK"} default button 1 return end if set mySource to the source of front document as text if (length of mySource is 0) then display dialog "You need to open a web location first!" buttons {"OK"} default button 1 return end if set myName to name of front document as text end tell
try tell application "TextMate" to activate set myName to replace_chars(myName, "http://", "") set myName to replace_chars(myName, "/", ":") if (myName ends with ".html") or (myName ends with ".htm") then set myPath to "/tmp/" & myName else set myPath to "/tmp/" & myName & ".html" end if do shell script "rm -f " & quoted form of myPath do shell script "echo " & quoted form of mySource & " >> " & quoted form of myPath do shell script "open -a TextMate " & quoted form of myPath end try
on replace_chars(this_text, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replace_chars ======================================================================= ==
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
I've been testing out TextMate for a week now, and really like what I've seen thus far. I'm using it for php and actionscript (so far.) Is there a tidy feature somewhere that I'm missing?
Thanks in advance.
On Thu, 3 Mar 2005 20:25:46 +0100, Allan Odgaard allan@macromates.com wrote:
On Mar 3, 2005, at 20:19, Nednieuws wrote:
TextMate already has such a command for Safari, although it just reloads, it doesn't ask. I just extended the command to favor OmniWeb, if that browser is already running.
I envision a configuration option for browser preference (maybe primary and secondary) and Cmd-R/Cmd-Opt-R :).
Well, having it check which of the browsers are running and just use the one which is, is probably desired in 99% of the cases (I have an aversion toward different hot keys and settings).
I guess the script could be smarter and detect when multiple browsers are running. present a dialog where the user could choose, and even make that choice sticky (see the recent get_doc_path.sh script I added to the repository for an example of how to have bundle items manage settings).
On 04.03.2005, at 00:07, Tijs wrote:
I'm a bit new to the OS X platform (already i can't do without textmate!) but shouldn't it be possible to detect the default browser and refresh that. After i picked a different default browser, Firefox in my case, some apps seem to notice and open firefox instead of safari when a browser is needed (netnewswire for instance).
It's not that easy.
From an AppleScript perspective, you can open a web page in the default browser with this command:
open location "http://www.example.com/"
However, opening a web page is the only thing you can instruct the default web browser to do with a simple generic command. Anything more complicated than that and you have to specifically script a particular browser, according to the interfaces made available in its AppleScript dictionary.
Looking at the AppleScript dictionary for Safari, the only way there appears to be to reload a window would be by sending it JavaScript commands, and I can't get that to work right now. Firefox has practically no AppleScript support whatsoever. OmniWeb does appear to have a reload command in its AppleScript dictionary. I haven't checked the other browsers.
Bypassing the AppleScript dictionary, one could instead script the application by telling to directly choose menu commands from the menubar. This requires some system-wide preference to be enabled, and I forget what it's called or where to find it.
In any case, you would have to have code that determines what the current browser is, and then have a special case for each browser you want to support in which you send it the correct AppleScript or menu commands to do a reload.
As you see it is a somewhat involved exercise.
On Mar 4, 2005, at 0:29, Mark Windrim wrote:
I've been testing out TextMate for a week now, and really like what I've seen thus far. I'm using it for php and actionscript (so far.) Is there a tidy feature somewhere that I'm missing?
Maybe you'll find this useful: http://lists.macromates.com/pipermail/textmate/2004-December/ 001673.html