If I have an applescript snippet such as this
tell application "Finder"
set thePath to (POSIX path of (path to application "MyApp"))
end tell
it will return to me
"/Applications/MyApp.app"
Now, what I can't seem to figure out is how to instead specify "MyApp" via a variable rather than the literal.
My applescript reads in some XML values, one of them being the name of the application I'm interesting in. I've tried this:
tell application "Finder"
set thePath to (POSIX path of (path to application someVariable))
end tell
but this simply tells me the error
"Finder got an error: Can't make application "MyApp" into type constant."
Any ideas how I can do this?
The answer (or at least one answer) is:
set theApp to "MyApp"
set pathToTarget to POSIX path of (path to application theApp)
Since path to application is a part of Standard Additions, the Finder is not needed.
Thanks to Stephan K on MacScripter for setting me straight on this.
Related
I'm making a small script that needs to use the name of the parent directory of the script itself (.scpt file) a variable.
For example, the script is located at /Users/spongebob/MyProject/myscript.scpt
I need to set the variable called myprojectdir to MyProject.
I've tried
set myprojectdir to parent of POSIX path of me
and other variations of this based on search results but I always end up with an error
Can’t get POSIX path.
Where am I going wrong?
Thanks
AppleScript itself has no idea.
You have to ask System Events
tell application "System Events" to set myprojectdir to name of container of (path to me)
or the Finder
tell application "Finder" to set myprojectdir to name of parent of (path to me)
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)
I'm having trouble accessing this file while trying to select it on the beginning characters basis...
set location to "/Users/myuser/Desktop/"
set bom to POSIX file (location & (first file of location whose name begins with "thisFile"))
tell application "Preview" to open bom
is it path/alias vs text type of a thing?
Only System Events and the Finder know what a file in the file system is.
The Finder has a property desktop which points always to the desktop of the current user.
tell application "Finder" to set bom to first file of desktop whose name begins with "thisFile"
tell application "Preview" to open (bom as alias)
Or with an arbitrary POSIX path
set location to POSIX file "/Users/myuser/Desktop" as text
tell application "Finder" to set bom to first file of folder location whose name begins with "thisFile"
tell application "Preview" to open (bom as alias)
The alias coercion is needed because Preview doesn't recognize Finder file specifier objects.
vadian's answer works well, but it's worth mentioning that:
you can get access to well-known folders even in the default context, outside the context of System Events and Finder; e.g.:
path to desktop
path to home folder
Use, e.g., POSIX path of (path to home folder) to get the POSIX path.
using context System Events is usually preferable to the Finder context, for reasons of both performance and predictability.
With an arbitrary target folder, using a POSIX path:
tell application "System Events"
set targetFolder to alias "/Users/jdoe/Desktop"
# equivalent of: set targetFolder to (path to desktop)
set targetFile to first file of targetFolder whose name starts with "thisFile"
end tell
tell application "Preview" to open targetFile
Alternatively, if you know your way around the shell, you could try:
set targetFilePosixPath to do shell script "fls=(~/Desktop/*.pdf); printf %s \"$fls\""
tell application "Preview" to open (POSIX file targetFilePosixPath as alias)
I'm attempting to set the current desktop picture to an image in the current directory of the script. For example, if my folder structure is:
~/Documents/Scripts/DesktopImageScript/image.jpg
With the script in the same directory, I want to be able to set the desktop image to the image.jpg without directly referring to the folder structure. The code I am currently using to fetch the current directory is:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
The issue isn't in that code as I can run the following command with the expected and correct results:
do shell script "echo " & app_directory
I believe the issue is in the code I'm using to set the desktop image:
tell application "Finder"
set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg"))
end tell
The error I receive when I try to run the script is:
error "Finder got an error: AppleEvent handler failed." number -10000
Not really sure what could be causing the error or how to fix it. Any help is appreciated. The full script is below:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
tell application "Finder"
set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg"))
end tell
Solved by using code from another answer on StackOverflow.
tell application "System Events"
set theDesktops to a reference to every desktop
repeat with x from 1 to (count theDesktops)
set picture of item x of the theDesktops to app_directory & "/image.jpg"
end repeat
end tell
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.