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)
Related
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.
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 want to change a filename in the folder which is not always the same, it depends on where the applescript is stored (same folder as the file to change).
I made this script with a dialog to check the path, that works fine but after but I get an error (-1700, Can't change "test" into an integer. Why, and how do I fix this?
tell application "Finder"
set thePath to POSIX path of ((path to me as string) & "::")
display dialog thePath buttons {"Clipboard", "OK"} default button 2
if the button returned of the result is "Clipboard" then
set the clipboard to thePath
end if
set name of document file "test" of thePath to "test_OLD"
end tell
If you are using Finder, which you only need for the set name statement, you need to coerce thePath from a posix path to a hfs path.
You can also remove the entire Finder block and use:
tell application "System Events" to set name of file (thePath & "test") to "test2"
Could this be an easier way syntax wise?
tell application "System Events"
set name of file "/Users/Firebird/Documents/test2.jpg" to "/Users/Firebird/Documents/11.jpg"
end tell
This question may seem strange. However I'm in the need of interacting with an application that is releasing worldwide with different names, eg. AppEN, AppGB, AppDE etc...
I am looking for a solution that allow me to use this command:
tell application process "AppnameGB"
However it should work with all the different variations of this application. I don't know if this is possible but searching for a string in the application name could do the trick: tell application process that contain in its name "Appname".
If the process is already open, you can use something like this:
tell application "System Events"
tell (process 1 where name starts with "TextEd")
properties
set f to its file
end tell
end tell
tell application (f as text)
properties
end tell
Telling Finder to list files is really slow:
tell application "Finder"
item 1 of (path to applications folder) where name starts with "Text"
end tell
You can use do shell script though:
set a to do shell script "ls /Applications/ | grep -m1 '^Text.*\\.app$'"
tell application a
properties
end tell
set a to do shell script "mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName==Text*' | head -n1" would also search outside the applications folder.
The easiest is if you could use the bundle identifier of the application instead of its name. Most likely the bundle ID doesn't change while the name does. So use this script on that application and check if the bundle ID changes or not.
tell application "Finder" to set bundleID to id of (choose file)
If you find it doesn't change then you can access it via applescript like this...
tell application "com.bundle.id"
activate
-- do something
end tell
Your only other alternative is get a list of all applications, and loop through them checking the name as you suggest. Something like this wold work but is pretty slow.
-- use spotlight to find all the apps on the computer
set cmd to "mdfind 'kMDItemContentType == \"com.apple.application-bundle\"'"
set foundApps to paragraphs of (do shell script cmd)
-- search the found apps looking for the one you're interested in
set appPath to missing value
repeat with i from 1 to count of foundApps
if (item i of foundApps) contains "Appname" then
set appPath to item i of foundApps
exit repeat
end if
end repeat
if appPath is missing value then error "Couldn't find the app!"
-- open the app
set macAppPath to (POSIX file appPath) as text
tell application macAppPath
activate
-- do something
end tell
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.