Some Path Finder AppleScripts

by Martin on 6/03/2009

Path FinderHow’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.


The scripts (clicky clicky):
 
Center Window
Open in TextMate
Create Project
**Bonus: Make AppleScripts readable in QuickLook/Path Finder preview.

Center window ?

A deceptively simple code snippet that I’ve posted before. It simply moves and resizes the current window (in most applications) so that any misshapen or lost window is ready to use again. By far the easiest to get to work right again. Just a simple check for the app and then use its bounds property to do the resizing and positioning rather than using oh, lets see; A resize and a position method? Grumble…

tell application "System Events"
   set frontmostProcess to name of first item of ¬
      (processes whose frontmost is true)
   if frontmostProcess is "Path Finder" then
      tell application "Path Finder"
         set bounds of finder window 1 to ¬
            {250, 130, 1200, 800}
      end tell
   end if
   tell process frontmostProcess
      tell window 1
         set position to {100, 0}
         set size to {1200, 800}
      end tell
   end tell
end tell

Open in TextMate ?

This is an all-time favorite usually implemented with a button in the top bar of the Finder. Click the button and the current selection is opened in TextMate. Buttons suck! Keyboard hotkeys rock!

if application "Path Finder" is frontmost then
  tell application "Path Finder"
    set pathfinderselection to selection
    if pathfinderselection is missing value then
      display dialog "Nothing selected" buttons ¬
        {"Oh, crud..."} default button 1
      return
    end if
    set pathfinderpaths to ""
    repeat with i from 1 to count of pathfinderselection
      set pfItem to item i of pathfinderselection
      -- Corrects for error when hidden files are showing
      try
        set currentpathfinderpath to (POSIX path of pfItem)
        set pathfinderpaths to pathfinderpaths & space ¬
          & quoted form of currentpathfinderpath
      end try
    end repeat
    do shell script "mate " & pathfinderpaths
  end tell
else if application "Finder" is frontmost then
  tell application "Finder"
    try
      if selection is {} then
        set finderSelection to folder of the ¬
          front window as string
      else
        set finderSelection to selection as alias list
      end if
    on error
      display dialog "Nothing selected" buttons ¬
        {"Curses, foiled again!..."} default button 1
      return
    end try
  end tell
  OpenFinderSelection(finderSelection)
end if

on OpenFinderSelection(listOfAliases)
  tell application "TextMate"
    open listOfAliases
    activate
  end tell
end OpenFinderSelection

Add as a quicksilver action or trigger and you’re all set. The selection opens in TextMate no matter what flavor of Finder you’re in. Why the old method won’t work for both Finders I have no idea, but after quite some trial and errors this solution seems to work.

Create “X” project ?

Another of my favorite scripts is one that I’ve created in multiple versions. Whenever I start a new project there’s usually the same folder structure for every Flash project, a slightly different for a Flex SDK project, and another for any complete site project. So I’ve created some project templates and at any given moment I invoke a Quicksilver action to set up a new project ready with all the necessary files (also the files a TextMate template cannot to my knowledge generate) and opens an appropriately named TextMate project. Like so:

TextMate Project

For some unfathomable reason this was an even bigger pain in the ass than the “open in TextMate” script. Enough talk. Code:

set currentFinder to GetCurrentApp()

if currentFinder is equal to "Path Finder" then
   CreatePathFinderProject()
else if currentFinder is equal to "Finder" then
   CreateFinderProject()
else
   display dialog "This script is only valid for Finder or Path Finder" ¬
      buttons {"Aw shucks"} default button 1
end if

on CreatePathFinderProject()
   tell application "Path Finder"
      set pathfinderselection to selection
      if pathfinderselection is missing value then
         display dialog "Nothing selected" buttons {"Doh!"} ¬
           default button 1
         return
      end if
      set pathfinderpaths to ""
      repeat with i from 1 to count of pathfinderselection
         set pfItem to item i of pathfinderselection
         try
            set currentpathfinderpath to (POSIX path of pfItem)
            set pathfinderpaths to pathfinderpaths & space & ¬
               quoted form of currentpathfinderpath
         end try
      end repeat
      set thefoldername to text returned of (display dialog ¬
         "Project name:" default answer "FlashProject")
      set newprojectpath to currentpathfinderpath & "/" & thefoldername
      set oldname to newprojectpath & "/FlashProject.tmproj"
      set newname to newprojectpath & "/" & thefoldername & ".tmproj"
      do shell script "mkdir " & newprojectpath
      do shell script ¬
      "cp -r /Users/YOUR_USERNAME/ProjectTemplates/FlashProject/ " & ¬
         "/" & newprojectpath
      do shell script "cd " & newprojectpath
      do shell script "mv " & oldname & " " & newname
      do shell script "open " & newname
   end tell
end CreatePathFinderProject

on CreateFinderProject()
   try
      tell application "Finder" to set the currentfinderselection ¬
         to (folder of the front window) as alias
   on error -- no open folder windows
      set the currentfinderselection to path to desktop folder as alias
   end try
   tell me to activate
   set thefilename to text returned of (display dialog ¬
      "Project name:" default answer "FlashProject")
   set thefullpath to POSIX path of currentfinderselection
   set newprojectpath to thefullpath & "/" & thefilename
   set oldname to newprojectpath & "/FlashProject.tmproj"
   set newname to newprojectpath & "/" & thefilename & ".tmproj"
   do shell script "mkdir " & newprojectpath
   do shell script ¬
   "cp -r /Users/YOUR_USERNAME/ProjectTemplates/FlashProject/ " ¬
      & "/" & newprojectpath
   do shell script "cd " & newprojectpath
   do shell script "mv " & oldname & " " & newname
   do shell script "open " & newname
end CreateFinderProject

on GetCurrentApp()
   tell application "System Events"
      set _app to item 1 of (every process whose frontmost is true)
      return name of _app
   end tell
end GetCurrentApp

Note that for this to work you need to store your template TextMate projects and files as indicated below. If you want a different file setup you’ll have to modify the script accordingly.

Script Project Structure

View AppleScripts in QuickLook ?

Really just more of a quick tip than anything else. I don’t really use QuickLook all that often, but I noticed that both when using QuickLook and when using the preview pane in Path Finder my scripts would just show an icon rather than the text content of the file. Like so:

Quicklook Applescript Boo

While what I’d like of course, is something more along the lines of this:

Quicklook Applescript Yay

The trick here is to save your files as “Text” rather than “Application” or “Script”.

Applescript Save as Text

Your files will get the extension “.applescript” instead of “.scpt”, and now be readable and still working as before. I’ve done some searching as to why this may be a bad idea, but so far I’ve come up short. If anyone has an idea, let me know.

In the end I may decide to not hang on to Path Finder, but at least I had some fun squashing bugs.

There are 2 comments in this article:

  1. 25/07/2009Josh says:

    great stuff, thank you.
    You mention above to add the script as an action? how do I do this. this has me stumped.
    I have a file for folder selected in pathfinder, how do I open it in textmate?
    I can do “cur”(current selection) tab “wi” and then “ma” (textmate). but is there a better way. your script seems to say YES but how. I always find myself obsessed with cutting just one more keystroke….

  2. 25/07/2009Josh says:

    forget it, just saw number 5 above
    http://ctrloptcmd.com/archives/370/quicksilver-actions-the-easy-way/

Write a comment: