PDA

View Full Version : AppleScript Tips


maaku
May 21, 2004, 02:51 PM
If you have a Finder window opened by some application which is not Path Finder aware, you may find the following AppleScript helpful:

on run
tell application "Finder"
activate
set this_folder to (the target of the front window) as alias
close the front window
end tell
tell application "Path Finder"
activate
open this_folder
end tell
end run

Paste it into Script Editor or the like, save it as an application with a shortish name in a convenient spot (e.g., /Applications/PF ), choose View->Customize Toolbar... in the Finder, and drag install it. Then when you click on it in the toolbar of a Finder window, it will close the Finder window and open a Path Finder one to the same directory.

flip
Apr 18, 2006, 10:39 PM
I've done some experiments scripting PF (4.1), and here's what I've found to be working so far.

To FileVault users: see the note at the bottom of this message.

I'll edit this message as I find more, and it would be cool if others posted here what they find too. However, to keep this thread usable, I'd suggest to reserve it to such tips and post questions and discussions in separate threads.

Accessing or referencing browser windows
Use "finder window"
tell application "Path Finder"
set windows_list to every finder window
get name of finder window 1
get bounds of front finder window
end tell

Changing the view of a browser window
Use "current view"
tell application "Path Finder"
set x to current view of front finder window
set current view of front finder window to "column view"
set current view of front finder window to "list view"
set current view of front finder window to "icon view"
end tell
Note that unlike the Finder, Path Finder expects a string.

Opening a new browser window
This should be doable by running "open new finder window", but it fails. The workaround is to use either
PFOpen <fsFolder | POSIX path | alias>,
open <alias | folder | list of aliases | list of folders>, or
reveal <fsItem | POSIX path | alias> (assuming the Reveal preference is set to "Reveal in new window")
tell application "Path Finder"
PFOpen "/Users/<username>/Desktop" -- passing it a POSIX path
open (choose folder) -- passing it an alias
reveal (choose file) -- passing it an alias
end tell
Note that a new window won't open if the folder you pass as parameter to PFOpen and open is already open. Apparently, reveal doesn't have this limitation.

Opening a file
PFOpen <fsItem | POSIX path | alias>, or
open <alias | file | list of aliases | list of files>
tell application "Path Finder"
PFOpen "/Users/<username>/Desktop/test.txt" -- opens the file
open (choose file) -- opens the file (as alias, here)
end tell

Showing a file or a folder in a new window
reveal <fsItem | POSIX path | alias> (assuming the Reveal preference is set to "Reveal in new window")
tell application "Path Finder"
reveal (choose file) -- opens a new window and selects the file (as alias, here)
end tell

Showing a file or a folder in the front window
select <fsItem | POSIX path | alias>
tell application "Path Finder"
select "/Users/<username>/Desktop" -- selects and displays the folder in the front window
end tellNote that at the moment it doesn't seem to be possible to select several items at once.

Opening a PF's Info window
PFInfo <fsItem | POSIX path | alias>
tell application "Path Finder"
PFInfo "/Users/<username>/Desktop" -- passing it a POSIX path
PFInfo (choose file) -- passing it an alias
end tell

Getting the selection
get selection
tell application "Path Finder"
set selection_list to selection -- list of fsItems (fsFiles and fsFolders)
end tell

Converting a fsItem, fsFile or fsFolder into anything (alias, Finder item, etc)
A fsItem is a PF's reference (the equivalent of Finder's item). fsFile is equivalent to file and fsFolder is equivalent to folder.
PF cannot coerce them into their Finder equivalent at the moment, but it can be done by coercing them first into a POSIX path (slash delimited string), then into a POSIX file (colon delimited strings). Note that the POSIX file and the alias coercions cannot be done when talking to Path Finder, though.
tell application "Path Finder"
set selection_list to selection
repeat with one_item in selection_list -- one_item is a fsItem (fsFile or fsFolder)
set a_path to POSIX path of one_item -- gets a posix path
tell me to set a_path to POSIX file a_path -- gets a colon delimited path string
tell me to set a_alias to alias a_path -- gets an alias
tell application "Finder" to set a_file to file a_path -- here we get a Finder reference
end repeat
end tell

Converting something into a fsItem
Unfortunately it doesn't seem to be possible at the moment to convert anything (alias, POSIX path, path string, etc) into a fsItem.
A workaround, while waiting for this bug to be fixed, would be to use select and selection:
set folder_alias to choose folder -- an alias
set a_path to POSIX path of folder_alias -- a slash delimited path string
tell application "Path Finder"
PFOpen a_path -- open the folder in a new window
select a_path -- select it in the new window
set selection_list to selection -- get the selection (always a list of fsItems)
close finder window 1 -- close the new window
repeat with one_item in selection_list
PFInfo one_item -- one_item is a fsFolder
end repeat
end tell

However this shouldn't be necessary, most of the time, because most commands will accept a POSIX path and an alias, or you'll already have a fsItem:
tell application "Path Finder"
set selection_list to selection
repeat with one_item in selection_list -- one_item is a fsItem (fsFile or fsFolder)
PFInfo one_item
end repeat
end tell

Getting the contents of a folder
PF is supposed to support "get every fsItem of <fsFolder>" but it doesn't work at the moment. You have to get fsFiles and fsFolders separately:
tell application "Path Finder"
set selection_list to selection
repeat with one_item in selection_list
if class of one_item = fsFolder then
set files_list to every fsFile of one_item
set folders_list to every fsFolder of one_item
end if
end repeat
end tell

Important note to FileVault users:

There is a bug (or is it a feature?) that leads AppleScript to get broken aliases and file paths if your user account uses FileVault. For example, the following script will work fine on a regular account, but will fail on a FileVaulted one.

tell application "Finder"
set theSelection to get selection
end tell
tell application "Path Finder"
repeat with i in theSelection
reveal i as string
end repeat
end tell

You can see the problem described at ScriptBuilders.net (http://scriptbuilders.net/files/filevaultprooffinderselectiontoaliaslist1.2.html), and a script that works around this limitation is provided on the same page.

To get the previous sample script working with FileVault, you'd replace the whole "on run" block of the ScriptBuilder's script with:

set theSelection to finderSelectionToPOsixPathList()
tell application "Path Finder"
repeat with i in theSelection
reveal i
end repeat
end tell

To be continued.

flip
Apr 20, 2006, 04:30 PM
Some people have asked how to simulate a dual pane configuration using AppleScript. This is something I wanted too, so here's my solution.

property win1bounds : missing value
property win2bounds : missing value

tell application "Path Finder"
set resetWindows to false
set firstRun to (win1bounds = missing value or win2bounds = missing value)
set openWinsNum to (count of (every finder window))
if firstRun and (openWinsNum = 0) then -- stop here
display dialog "Open a browser window and try again." buttons {"Cancel"} default button "Cancel" with icon 0
end if
if not firstRun and openWinsNum > 0 then
set winName to name of finder window 1
display dialog "Reset the saved windows using the front window (\"" & winName & "\") as new reference, or ignore the front window and open two new windows?" buttons {"Reset", "Ignore"} default button "Ignore" with icon 2
if button returned of result = "Reset" then set resetWindows to true
end if
if firstRun or resetWindows then
-- use window 1 as reference and open window 2
getAndSaveBounds() of me
openWindow(win2bounds) of me
else -- open two new windows
openWindow(win1bounds) of me
openWindow(win2bounds) of me
end if
activate
end tell

on openWindow(winBounds)
tell application "Path Finder"
set homeDir to (path to "cusr" from user domain) -- home (alias)
reveal homeDir
set bounds of finder window 1 to winBounds
end tell
end openWindow

on getAndSaveBounds()
-- the bounds are saved in the properties
tell application "Path Finder"
set win1bounds to bounds of finder window 1
set winL to item 1 of win1bounds -- left
set winT to item 2 of win1bounds -- top
set winR to item 3 of win1bounds -- right
set winB to item 4 of win1bounds -- bottom
set winW to winR - winL -- width
set winH to winB - winT -- height
if winW > winH then -- stacked windows
set win2bounds to {winL, winB, winR, winB + winH}
else -- juxtaposed windows
set win2bounds to {winR, winT, winR + winW, winB}
end if
end tell
end getAndSaveBounds

Installing it

Launch ScriptEditor, paste the script into a new document, and save it.

If you're using Apple's Script Menu (my preference), save the script as compiled script into ~/Library/Scripts/Applications/Path Finder/ (create the folders if necessary). It will then be accessible in the Script menu each time PF is frontmost. If you're not using Script Menu yet but want to try it, launch /Applications/AppleScript/AppleScript Utility and enable it from there.

You can also save the script as an application, and add it to PF's toolbar.

Finally, make sure that Reveal in new window is checked in the Reveal prefs pane.

Using it

The first time you launch this script, it uses the front window to determine the bounds (size and position) of the second window. Then it opens and positions that second window. If the front window's width is greater than its height, the windows will be stacked (with the second one at the bottom). If the front window's height is greater than its width, the windows will be side by side (with the second one on the right side). The bounds of both windows will then be saved (until the script is recompiled).

The next times you'll use the script, if no window is already open, it will just open two new windows and set their bounds to the saved ones. If at least one window is already open, though, you'll be prompted to use it as new first window (determining and saving again the bounds of the second one), or to ignore it and open two new windows, using the previously saved bounds. That way you can easily switch between stacked and side-by-side windows, and your last choice is always saved.

Enjoy!

flip
Apr 22, 2006, 01:52 PM
Running scripts from the contextual menu

Ranchero Software has a very cool freeware contextual menu plugin named BigCat which is compatible with Path Finder: http://ranchero.com/bigcat/.

It gets the selection from PF as a list of aliases, just like it does in the Finder. This is great, because it allows some scripts to run from both applications.

For example, here's a script that will open in a single BBEdit window the files selected in the Finder or in PathFinder (assuming you're using a version of BBEdit recent enough to be able to open several files in one window):
on main(file_list)
tell application "BBEdit"
open file_list opening in new_window
tell text window 1
set show documents drawer to true
set show navigation bar to true
activate
end tell
end tell
end main

And here's another example that will reveal and select in the Finder the items selected in PF (whose current "Reveal in Finder" command selects only the first item of multi-selections):
on main(file_list)
tell application "Finder"
reveal file_list
select file_list
activate
end tell
end main

Note that BigCat can also run shell scripts!

Hamster
Aug 22, 2006, 04:31 AM
Impressive. Big thx.

Regards, Hamster