(archive 'newLISPer)

December 9, 2005

Choosing a file on MacOS X

Filed under: newLISP — newlisper @ 12:59
Tags:

For one task, I want to choose a file interactively, using the Mac’s
Open File dialog (which contains SpotLight searching powers too).

The only way to do this at the moment that I can find involves
calling AppleScript, via the osascript system command. This is
slightly harder than easy, because you’ve got to quote the files
correctly if they’re to survive their journey through the Finder, the
shell, and your newLISP code. Luckily newLISP provides us with some
useful tools for protecting both the AppleScript code and the file
names (which can also contain spaces).

(define (choose-file-with-prompt prompt)
     (exec (format {osascript -e 'tell application "Finder"
     activate
     set af to choose file with prompt "%s"
     end tell
     set pf to POSIX path of af ' } prompt )))
(set 'f (choose-file-with-prompt "Choose an XML file"))
(println (xml-parse (read-file (first f))))
Advertisements

4 Comments »

  1. >Hi,Im new to newlisp and was wondering if you could demonstrate how to rewrite the choose-file-with-prompt function into two functions, 1 that accepts the applescript and format paramters and one that passes the applescript and paramters to that function. In other words you would have something like:(define (osascript applescript optional-format-paramters)???(define (choose-file-with-prompt prompt)(osascript {tell application “Finder” activate set af to choose file with prompt “%s” end tell set pf to POSIX path of af} prompt))

    Comment by Anonymous — June 12, 2006 @ 10:02 | Reply

  2. >This is a start:(define (osascript applescript) (exec (format {osascript -e ‘%s’} applescript )))(define (choose-file-with-prompt prompt) (set ‘s (format [text]tell application “Finder”activateset af to choose file with prompt “%s”end tellset pf to POSIX path of af[/text] prompt))(osascript s))(println (choose-file-with-prompt “please”))(println (choose-file-with-prompt “dont”))The problem is the quotes – things always get messy because you want to keep any double quotes and curly braces in AppleScript, which you can protect by enclosing in single quotes, but then you can’t use single quotes in the AppleScript … I’ve used the [text] [/text] tags here, which are useful alternatives to the other string delimiting tags…

    Comment by newlisper — June 12, 2006 @ 10:48 | Reply

  3. >Thanks. I was thinking of passing applescript and paramters rather than the fully formed applescript , duh!Not at my Mac at the moment but I think:(define (choose-file-with-prompt prompt)(osascript (format [text]tell application “Finder” …..Should work without having to set ‘s?

    Comment by Anonymous — June 12, 2006 @ 11:44 | Reply

  4. >Oh yeah, this is just the way I work – expanded somewhat at first, so that alternatives can be quickly tested/replaced/commented/logged without requiring much editing. When it’s all working, compress it down to ‘haiku’-style dimensions…These techniques should work with osascript arguments too – trouble is, you have to write your AppleScript to take arguments:on run argv return “hello, ” & item 1 of argv & “.”end run

    Comment by newlisper — June 12, 2006 @ 12:53 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: