← blog

AppleScripts

So, I wrote a couple of AppleScripts and connected them to keyboard shortcuts to make my life easier. The first opens a terminal window with the working directory set to the current folder in Finder. The second opens the selected files in MacVim - this is particularly useful for html files.

To set this up, open Automator and choose the Service option.

Then search for Run AppleScript in Actions. Type in your code and choose the appropriate settings for where the script should run and what inputs it should receive. For the terminal script it should be: Service receives no input in any application. For the Vim script it should be: Service receives files or folders in Finder. Save your service. Now open System Preferences head to Keyboard then Shortcuts in the segmented control at the top. Choose Services in the left hend list, then scroll to your newly created script. Check the box and assign it a keyboard shortcut.

The script to open a new Terminal window in the current Finder location is:

tell application "Finder"
  set thisPath to (the POSIX path of (insertion location as alias))
  tell application "Terminal"
    activate
    tell window 1
      do script "cd " & quoted form of thisPath & "; clear"
    end tell
  end tell
end tell

The script to open selected files in MacVim is:

on run
  tell application "Finder" to set selectedFiles to selection as alias list
  set openCommand to "/usr/local/bin/mvim"
  repeat with selectedFile in selectedFiles
    set filePath to quoted form of (POSIX path of selectedFile)
    set openCommand to openCommand & " " & filePath
    set thePath to (characters 1 thru -((offset of "/" in (reverse of items of filePath as string)) + 1) of filePath as string) & "'"
  end repeat
  do shell script "cd " & thePath & "; " & openCommand
end run

Enjoy!