You should be automatically redirected . If not, visit
http://newlisper.wordpress.com
and update your bookmarks.

09/12/2005

Choosing a file on MacOS X

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))))

4 Comments:

At 11:02, Anonymous Anonymous said...

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))

 
At 11:48, Blogger newlisper said...

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"
activate
set af to choose file with prompt "%s"
end tell
set 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...

 
At 12:44, Anonymous Anonymous said...

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?

 
At 13:53, Blogger newlisper said...

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

 

Post a Comment

Links to this post:

Create a Link

<< Home