AppleScript - What's wrong with my quotes? - applescript

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.

Related

Reveal in finder does not work with concatenated string

Why does this work:
tell application "Finder"
activate
reveal POSIX file ("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/file.png")
end tell
...but not this
tell application "Finder"
activate
reveal POSIX file ("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/" & "file.png")
end tell
And how do I get it to work if I want to join a path (string) with a variable (string)?
System Events handles POSIX paths a lot better, but this is just another one of those AppleScript oddities. POSIX file will work outside of a Finder tell statement:
set x to POSIX file (pathVariable & otherPathVariable)
tell application "Finder"
activate
reveal x
end tell
but within a Finder tell statement you need to use it as a coercion:
tell application "Finder"
activate
reveal (pathVariable & otherPathVariable) as POSIX file
end tell
I recommend to use relative HFS paths. The first line points to the library folder of the current user.
set libraryFolder to path to library folder from user domain as text
tell application "Finder"
reveal file (libraryFolder & "com~apple~CloudDocs:MyFolder:" & "file.png")
end tell
try this to combine strings
reveal POSIX file (("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/" & "file.png") as text)

Relative path to AppleScript exported as .app

I have an AppleScript exported as a .app file because I need it to run on log in.
This is my code:
repeat
tell application "System Events"
repeat with desktopNumber from 1 to count of desktops
tell desktop desktopNumber
set picture to "~/Desktop/script/img.jpg"
end tell
end repeat
end tell
end repeat
The path to the script is ~/Desktop/script/script.app/Contents/Resources/Scripts/main.scpt
I'd like to put the image in the Resources folder as well and make the path relative so i can put the folder anywhere without changing my script so I tried
set desktopPicture to ((container of container of (path to me)) as text) & "/img.jpg"
repeat
tell application "System Events"
repeat with desktopNumber from 1 to count of desktops
tell desktop desktopNumber
set picture to desktopPicture
end tell
end repeat
end tell
end repeat
But that gives me the error Can’t make container of container of alias \"Macintosh HD:Users:Me:Desktop:script:script.app:Contents:Resources:Scripts:main.scpt\" into type text.
System Events is not able to expand the tilde ~.
If you want to refer to ~/Desktop/script/img.jpg using relative paths you could use
tell application "System Events" to set desktopPicture to file "img.jpg" of folder "script" of desktop folder
or
set desktopPicture to alias ((path to desktop folder as text) & "script:img.jpg")
Consider that in both cases AppleScript will throw an error if the file does not exist.

Open "Get Information" Window in Finder by Applescript (Again)

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

handler cannot handle objects of this class -10010

I was trying to use a script to open CS:GO with an AppleScript program, but I get the error message:
Handler cannot handle objects of this class -10010
Here's the script:
set myfilepath to "Macintosh HD:Users:Kasper:Desktop:Counter-Strike Global Offensive"
tell application "Finder" to open myfilepath
delay 5
tell application "System Events" to set unixID to unix id of process "csgo_osx"
do shell script ("renice -20 -p " & unixID) password "2306" with administrator privileges
Any ideas why this happens?
Your second line is telling Finder's Open command to open a string. It needs to be given a file or alias. Change that line to:
tell application "Finder" to open file myfilepath

Photoshop Scripting with Applescript — can't open image with "choose file"

I know I'm missing something incredibly simple here, but I can't figure out what it is. If I use the following script:
tell application "Adobe Photoshop CS5.1"
open file "path:to:some:file"
end tell
it works no problem, but if I try to use the "choose file" dialog everything falls apart:
tell application "Adobe Photoshop CS5.1"
set myFile to choose file
open myFile
end tell
I get an error
error "Adobe Photoshop CS5.1 got an error: File some object wasn’t found." number -43
What am I missing here?
For some reason, Photoshop CS5.1 dropped support for opening alias based file references.
You'll need to convert the path to a string and prefix the myFile variable with file.
tell application "Adobe Photoshop CS5.1"
set myFile to (choose file) as string
open file myFile
end tell
It works in Photoshop CS3. I don't have CS5 so I can't test it.
An easier alternative would be to use:
tell application "Adobe Photoshop CS3"
activate
tell application "System Events"
key code 31 using command down
end tell
end tell
You could also try Automator.

Resources