There's a question on the site about getting file information via script, well answered by regulus6633. The solution provided worked for me on a folder, but I was unable to get it to work similarly on an application file.
set aFile to (POSIX file "/Applications/Google Chrome.app") as text
tell application "Finder" to open information window of application aFile
The path is correct, insofar as I can tell, but it keeps returning the syntax error:
"Finder got an error: Can’t get application "Macintosh HD:Applications:Google Chrome.app".
(I'm a designer, not a coder, so please excuse if the answer's obvious!)
You almost had it right, but "almost" can be frustrating in AppleScript :-)
set aFile to (POSIX file "/Applications/Google Chrome.app") as alias
tell application "Finder" to open information window of aFile
To get the data, rather than opening the window to look at, use
set aFile to (POSIX file "/Applications/Google Chrome.app") as alias
info for aFile
Related
Hello and thanks for your help,
This is driving me crazy! I keep having syntax errors for simple stuff like just opening a file.
I get > Syntax Error - Expected end of line, etc. but found “"”.
What am I doing wrong? It's the same quotes as the ones for Finder...
tell application "Finder"
set theFile to selection
open theFile with application "QuickTime Player"
end tell
Here is the corrected script[1]:
tell application "Finder"
set theFile to selection
open theFile using application file "QuickTime Player" of folder "Applications" of startup disk
end tell
This can be simplified by specifying the application by bundle ID instead of location, in which case Finder will find it for you:
open theFile using application file id "com.apple.QuickTimePlayerX"
Or, if you want to do stuff with the file(s) once they’re opened:
tell application "Finder"
set theFiles to selection as alias list
end tell
if theFiles is {} then error "No files selected."
tell application "QuickTime Player"
activate -- (ensures any "can't open file" error dialogs are visible)
open theFiles
-- do other stuff here
end tell
--
[1] The syntax error is due to the special behavior of with/without parameter labels, which expect to be followed by a single keyword (or comma-separated keywords). AppleScript reads the command as open theFile using application—and since a command can’t be immediately followed by an expression, in this case a literal string, it reports a (frustratingly unhelpful) syntax error.
The following works in Script Editor (or an Applescript App), but not in XCode:
tell application "Finder" to set folder_list to items of folder POSIX file "/Users"
Specifically, I get at runtime:
Finder got an error: Can’t make «class ocid» id «data optr000000002094230000600000» into type integer. (error -1700)
If I try "double coercion":
...((folder POSIX file "/Users") as POSIX file)
I get:
Can’t make «class cfol» «script» of application "Finder" into type POSIX file. (error -1700)
I did see something similar discussed here, but the solution did not work for me:
"POSIX file" works in Applescript Editor, not in XCode
Thanks!
//Reid
p.s. I know I could just use "Macintosh HD:Users"... This works, unless somebody renamed their hard drive.
Applescript has the "path to" command to find paths to well known folders, like a user's home folder, desktop, documents folder etc. That's the best way to access the folders. You can see all the locations applescript knows in the Standard Additions applescript dictionary for the "path to" command. It also knows where the users folder is. As such I would write your code like this...
set usersPath to path to users folder
tell application "Finder" to set folder_list to items of usersPath
You can try to smoothen it out yourself, as I suspect the AppleScript editor does for you:
tell application "Finder" to set folder_list to items of folder (POSIX file "/Users" as text)
What I did, was to coerce the posix file to text, otherwise it is of class furl which really isn't what Finder can take. I'd try to coerce your posix file statements to text, and see if that helps.
This compiles and runs, both from within my Editor, and from the script menu:
tell application "Xcode"
tell application "Finder"
set m to folder (POSIX file "/Users" as text)
set n to name of m
tell me to display alert n
end tell
end tell
I hope this helps.
I've some problems to find the correct apple script command: I want to open the info window for a given file or folder. It's that window which I can open via cmd+i in Finder. Now I want to be able to automate this action by script file.
My code actually looks like this:
set aFile to POSIX file "/Users/xyz/Documents/test.rtf"
tell application "Finder" to open information window of aFile
But that doesn't work. The error message says that the file "information window of Macintosh HD:Users:xyz:Documents:test.rtf" cannot opened.
Some commands are picky with posix file. So we can coerce that to something else and it will work...
set aFile to (POSIX file "/Users/xyz/Documents/test.rtf") as alias
tell application "Finder" to open information window of aFile
or
set aFile to (POSIX file "/Users/xyz/Documents/test.rtf") as text
tell application "Finder" to open information window of file aFile
This one-line Applescript works fine on my Mac:
tell application "Finder" to open POSIX file "/Development/Applescript/Rapport.docx"
However it fails from a trivial ApplescriptObjC project:
on applicationWillFinishLaunching:aNotification
-- Insert code here to initialize your application before any files are opened
tell application "Finder" to open POSIX file "/Development/Applescript/Rapport.docx"
end applicationWillFinishLaunching:
The error I get is:
*** -[CMDAppDelegate applicationWillFinishLaunching:]: POSIX file "/Development/Applescript/Rapport.docx" of «script» doesn’t understand the “open” message. (error -1708)
What am I missing?
I had the problem once and i can't explain this weird behavior but a little rearrangement worked for me:
tell application "Finder" to open "/Development/Applescript/Rapport.docx" as POSIX file
How does one open a file in the same folder as the AppleScript code? Something along these lines?
tell application "QuickTime Player"
activate
open "file.avi"
end tell
(which doesn't work).
Thanks!
tell application "Finder"
open file "somefile.txt" of folder of (file (path to me))
end tell
(only works once you've saved the script - otherwise "path to me" goes to the script editor)