How do You Open the Contents of a Folder with AppleScript?

2

I am currently using this UNIX shell command in my script file:

do shell script "open ~/Dropbox/Media/VisionBoard/*"

Is there another way to do it and if so would it be any better or different? I'm still new to programming, so please be gentle.

Orion751

Posted 2010-08-12T17:23:46.447

Reputation: 294

Answers

4

tell application "Finder"
    set myFolder to ((home as text) & "Dropbox:Media:VisionBoard") as alias
    set myFiles to (every item of myFolder) as alias list
    open myFiles
end tell

Instead of "every item" you can also say "every file" or "every folder".

ischeriad

Posted 2010-08-12T17:23:46.447

Reputation: 912

What's the significance of "as alias," and is that why we can't say open every item of ((home as text) & "Dropbox:Media:VisionBoard")? – NReilingh – 2010-08-12T18:42:11.847

I am no AppleScript guru, just a student. There may be many ways to do it.

From Apple's AppleScript Language Guide: “An alias object is a dynamic reference to an existing file system object. [...] The following is the recommended usage for these types:

* Use an alias object to refer to existing file system objects.
* Use a file object to refer to a file that does not yet exist.”

http://developer.apple.com/mac/library/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-DontLinkElementID_362

– ischeriad – 2010-08-12T19:18:10.857

3@NReilingh By default, Applescript treats the bracketed term as a string. The every keyword specifies every object in a container. Strings contain characters. If you try and open every item of a string, AppleScript will throw an error because it can't convert the characters into type item. Items in the Finder are files, folders, etc. Therefore you coerce the file path string to an alias, which is a reference to an existing file, folder, or volume in the file system. The Finder can then treat that alias as a container of file system items. – ghoppe – 2010-08-12T19:52:03.267

Thanks for the discussion. It gave me some great insights as to how AppleScript works.

The information is probably of no tangible use, but can anyone confirm my suspicion that running pure AppleScript is more efficient than using it to perform a shell script? – Orion751 – 2010-08-13T18:59:48.473