14/01/2010
I was just writing an AppleScript to look up words on the excellent NinjaWords and decided to post it in case someone else needs something similar. Since one snippet of AppleScript is not a very meaty post, I’ll throw in a couple extra.
Look up word on NinjaWords
Select a word and copy it to the clipboard. Then invoke this script via Quicksilver or LaunchBar or whatever is your favorite tool for invocations.
1 2 3 4
| set clip_url to (the clipboard as string)
set lc_url to do shell script "echo " & clip_url & " | tr '[:upper:]' '[:lower:]'"
set ninjaURL to "http://ninjawords.com/" & lc_url
do shell script "open " & ninjaURL |
Wrap link in ‘a href…’
Note: This one will look slightly different depending on whether you are using Quicksilver, LaunchBar, whatever. The key is that you pass a string value into the script and it returns one back to you. This example is for LaunchBar.
Copy a link to the clipboard, invoke the script, paste your now a-tagged link.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| on handle_string(vanillaStr)
set quote to ASCII character 34
set openTag to "<a href=" & quote
set closeTag to quote & ">"
set finishTag to "</a>"
set TempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to space & ":" & space
if (count of text items of vanillaStr) is greater than 1 then
set urlStr to text item 1 of vanillaStr
set linkStr to text item 2 of vanillaStr
set returnStr to openTag & urlStr & closeTag & linkStr & finishTag
else if (count of text items of vanillaStr) is 1 then
set urlStr to text item 1 of vanillaStr
set linkStr to "linkage"
set returnStr to openTag & urlStr & closeTag & linkStr & finishTag
else
beep
return
end if
set AppleScript's text item delimiters to ""
tell application "LaunchBar"
perform action "Copy and Paste" with string returnStr
end tell
end handle_string |
New File
Sometimes you just want a dang text file to magically appear.
1 2 3 4 5 6 7 8 9 10 11 12
| try
tell application "Finder" to set the this_folder ¬
to (folder of the front window) as alias
on error -- no open folder windows
set the this_folder to path to desktop folder as alias
end try
tell me to activate
set thefilename to text returned of (display dialog ¬
"Create file named:" default answer "filename.txt")
set thefullpath to POSIX path of this_folder & thefilename
do shell script "touch \"" & thefullpath & "\""
do shell script "mate \"" & thefullpath & "\"" |
That’s it for now. I have some gems saved for a later post, but they wouldn’t make sense out of context so you’ll just have to wait.
6/03/2009
How’s that for a TitleCased title?
So I’ve been checking out Path Finder again. The thing with Path Finder is that it’s very nearly awesome, but there are a few things that bug the hell out of me. I may or may not come back to the most irritating shortcomings in a later post, but I’m going to address one of the main issues straight away; Path Finder has crappy support for AppleScript.
I love that little quirky language that actually gives me loads of control over the OS, but for some reason you can’t just rewrite your old AppleScripts to do a check for the current “Finder app” and react upon it with the same code. I have no idea why porting the Finder Dictionary to the Path Finder Dictionary is so hard (if I did I’d be writing some way cooler code than I currently am) but it bugs the hell out of me when some of my most frequently used scripts won’t work anymore. It’s like sitting down on a Mac that hasn’t got Quicksilver installed. You feel like you’re typing with boxing gloves.
Therefore I’ve set out to port my most crucial scripts so they’ll work whether I’m in Finder or Path Finder.
Read the rest of this article »
5/02/2009
Scenario: You’re working with the Flex SDK and you’re tracing messages to the Terminal via flashlog.txt. The damn logfile has heaps of clutter in it from the last project you were working on, and you want to clean it up.
Solution: Save this snippet and assign it a Quicksilver hotkey:
1 2 3 4
| try
do shell script "rm ~/Library/Preferences/Macromedia/Flash\ Player/Logs/flashlog.txt"
do shell script "touch ~/Library/Preferences/Macromedia/Flash\ Player/Logs/flashlog.txt"
end try |
Result: You are happy. Go drink beer.
17/01/2009
Scenario: You are browsing in Safari (because it’s speedier than Firefox) and feel the urge to inspect the site in Firebug or the Web Dev Toolbar.
Solution: Save this script and activate it from QuickSilver.
1 2 3 4 5 6 7 8 9 10
| tell application "Safari"
activate
set theUrl to the URL in document 1
end tell
tell application "Firefox"
activate
OpenURL theUrl
end tell |
Result: You are happy. Have a coffee.
6/08/2008
I’ve been using Yojimbo, for a long while now but Eystein has been pestering me to give Evernote a shot. Common for both of these is that you don’t want to spend any time when you’re dropping stuff into them. I want one key that’ll toggle the visibility of the app. I want to hit my hotkey, dump stuff in, hit my hotkey again and continue what I was doing. Of course, neither of these apps come with that functionality (Well. Yojimbo does come with a “Quick input”-window, but for some reason I never got to loving it.) so here’s an AppleScript to fix that.
Just change the name in the script to whatever app you want to target, create a Quicksilver trigger for it and Bob’s your uncle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| on run
tell application "Finder" to set en to name of first ¬
process whose frontmost is true
if en = "Evernote" then
tell application "System Events" to set visible ¬
of process "Evernote" to false
set notHidden to false
else
tell application "System Events" to set visible ¬
of process "Evernote" to true
activate application "Evernote"
set notHidden to true
end if
end run |
I’m really enjoying messing around with AppleScript these last few days. For such a light-weight language the benefits of it commanding the OS are huge.