A few random AppleScript snippets

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.

Possibly related posts:

  1. Some Path Finder AppleScripts How’s that for a TitleCased title? So I’ve been checking...
  2. Center and resize window with AppleScript I use a 15″ MacBook Pro that I carry between...
  3. Clean flashlog.txt with a keystroke Scenario: You’re working with the Flex SDK and you’re tracing...
  4. Migrate from Things to The Hit List Edit: I updated the app to work on Snow Leopard....
  5. OmniFocus clippings from Firefox Summary: In which Martin has created an AppleScript to remedy...

2 Comments

  1. Christian
    Posted August 23, 2010 at 21:41 | Link

    This is great! A few months ago, after hours of fiddling around with the weird-to-learn language, I managed to shuffle together some working AppleScript lines to create a MarkDown file by triggering a key (as a Snow Leopard Service, that is):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    on run {input, parameters}
      set theTitle to text returned of (display dialog "Title" default answer "new file" buttons {"Create"} default button 1)
      set theName to (do shell script "date +%Y-%m-%d_%H%M") &amp; "_?_" &amp; quoted form of theTitle &amp; ".md"
      set theFile to null
      tell application "Finder"
        set theFolder to quoted form of POSIX path of (the insertion location as text)
        set theFile to do shell script "echo \"... header ... \" &gt; " &amp; theFolder &amp; theName &amp; "; echo " &amp; theFolder &amp; theName
        reveal theFile as POSIX file
      end tell
     
      return theFile
    end run

    I wonder how I came up with “POSIX path of …” and “quoted form of” and the like and actually make it work. With a few tweaks it now looks more like the following (relative) beauty of simplicity:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    on run {input, parameters}
      tell application "Finder" to set theFolder to (the insertion location as text)
      tell me to activate
      set theTitle to text returned of (display dialog "Title" default answer "new file" buttons {"Create"} default button 1)
      set theFileName to (do shell script "date +%Y-%m-%d_%H%M") &amp; "_?_" &amp; theTitle &amp; ".md"
      set theFullPath to POSIX path of (theFolder &amp; theFileName)
      do shell script "touch \"" &amp; theFullPath &amp; "\""
      tell application "Finder" to reveal theFullPath as POSIX file
      return theFullPath
    end run

    It even feels faster. But I guess that effect wears of if the script lies there for a while, unused. It’s weird how slow AppleScript execution is. Thanks for sharing your script!

  2. Martin
    Posted August 24, 2010 at 20:23 | Link

    I’m glad you found some use for some of this stuff. :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*