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)
Related
I want to pass the path of the containing folder of a file to a command make new file at path. I know how to get the path of the currently open file (with tell application "TexShop" to set thePath to get path of document 1), but I don't know how to get the path of the containing folder.
I tried using container of, like this:
tell application "TeXShop"
set thePath to get container of path of document 1
end tell.
But I get this error:
TeXShop got an error: Can’t make container of path of document 1 into type reference.
Is that because while the container command is allowed in the application Finder it is not for the application TeXShop?
This following AppleScript code will get the path to the front most document in the front most application and the path to its containing folder. This will also create the new file in the containing folder.
(* The Delay Command Gives You Time To Bring The Desired App To The Front
Mainly For Use While Testing This Code In Script Editor.app *)
delay 5 -- Can Be Removed If Not Needed
tell application (path to frontmost application as text) to ¬
set documentPath to (get path of document 1) as POSIX file as alias
tell application "Finder" to set containingFolder to container ¬
of documentPath as alias
tell application "Finder" to make new file at containingFolder
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)
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 just want to move an image from one folder to the other, replacing the one that's already in there:
tell application "Finder"
copy file "/Users/xx/Documents/img.jpg" to folder "/Users/xx/Documents/State"
end tell
When I run it, I get an error message saying
Finder got an error: Can’t set folder [path] to file [path]"."number
-10006 from folder [path]
Please help me!
Try:
tell application "Finder"
duplicate POSIX file "/Users/xx/Documents/img.jpg" to POSIX file "/Users/xx/Documents/State" with replacing
end tell
Or
tell application "Finder"
move POSIX file "/Users/xx/Documents/img.jpg" to POSIX file "/Users/xx/Documents/State" with replacing
end tell
As #adayzdone notes, the error appears because you're using a Posix-style path without declaring it.
Another approach is to use colon-separated HFS paths, like so:
move file "Macintosh HD:Users:xx:Documents:img.jpg" ¬
to "Macintosh HD:Users:xx:Documents:State:" with replacing
With colon-separated paths you need to include the whole thing, including the volume name (I'm assuming Macintosh HD here), otherwise it'll throw our good friend error 10,006.
It helped me:
set theSource to POSIX file "/Users/xx/Documents/img.jpg"
set theDest to POSIX file "/Users/xx/Documents/State"
tell application "Finder"
move theSource to folder theDest with replacing
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.