POSIX file works in tell block - xcode

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.

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)

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

OS X 10.9 Applescript changes: using the `move` command in the "System Events" context to move a file

I went to run an old script and it broke after the 10.9 update. I used to move files with system events with the following code.
set Somefilepath to "Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt"
set somefolderpath to "Design_005_HD:Users:Design_005:Desktop:End:"
tell application "System Events"
move file (Somefilepath) to folder (somefolderpath)
end tell
Now it gives me the following error.
error "System Events got an error: Can’t make file
\"Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt\" into
type integer." number -1700 from file
"Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt" to integer
I know I can swap it out and use finder but I rather not use it. What changed that is no longer works?
Update 4/2/14
I have tried this in every way of giving the file/folder location to system events and it doesn't work. I am glad it is just not me who cannot get it to work. I will update this post if I find an answer or a working update is made.
Update 4/3/14
It seems this is just a bug that system events can't move files. I have reported it here http://bugreport.apple.com/ and everyone else should too.
Please do not take my code to heart, it is just where things ended up when I couldn't get it to work. I have working code for 10.8.5 and it is what is shown above minus the folder tag in the system events tell block. No idea why it works with out the folder tag but it does. Tested on multiple comps. If it isn't broken don't fuss over it. Noted it and moved on.
Update 10/20/14
For anyone interested. I have received an e-mail stating my ticket has been closed/resolved. So mavericks will for ever be broken but there might be light for Yosemite when it comes out.
In general, Applescript works with colon delimited paths (:) not slash delimited paths (/). I say in general because some applications will work with slashes but all programs will work with colons. For an example of what the colon paths look like try this code and look at the result...
set colonPath to (path to desktop as text) & "untitled folder 2:"
So first I would convert your slashes to the colon style.
Also to applescript these are just strings not paths. To make applescript understand they are paths we can do a few things. First you can add the words file or folder in front of them as appropriate. I notice in your code you are using "file" in front of the file string but you aren't using "folder" in front of the folder string. So try that. Second you can just use "alias" in front of the strings whether they're files or folders. There are other ways as well but I'll stop here. Either of those ways should work.
UPDATE: with all of the above being said, it seems System Events in 10.9 still has trouble with the move command. As such here's 2 alternatives for you. I used slash style paths since that's what you're using.
set somefilepath to POSIX file "/Users/Design_005/Desktop/Start/TextFile.txt"
set somefolderpath to POSIX file "/Users/Design_005/Desktop/End"
tell application "Finder"
move somefilepath to somefolderpath
end tell
or
set somefilepath to "/Users/Design_005/Desktop/Start/TextFile.txt"
set somefolderpath to "/Users/Design_005/Desktop/End"
do shell script "mv " & quoted form of somefilepath & space & quoted form of somefolderpath
Good luck.
You can’t do that. System Events can delete and open, but not move. Here’s a simple example in case it helps someone else find a better answer in a future OS. System Events appears to treat move differently than delete and open.
tell application "System Events"
set myFile to file "Macintosh HD:Users:velma:Desktop:Test.png"
set myFolder to folder "Macintosh HD:Users:velma:Desktop:Test"
--delete works! with both type “file/folder” and type “disk item”
--delete myFile
--delete myFolder
--open works!
open myFile
open myFolder
--move fails!
move myFile to myFolder
end tell
The error it’s returning, in this case, is “Can’t get file”, number -1728.
There appears to be bug in the move command in the "System Events" context in OX 10.9 (and possibly 10.8).
The workaround is the to use the "Finder" context instead:
Using HFS-style paths (separator is :)
set somefilepath to "Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt"
set somefolderpath to "Design_005_HD:Users:Design_005:Desktop:End:"
tell application "Finder"
move file somefilepath to folder somefolderpath
end tell
Using POSIX-style paths (separator is /) - as in the original question
set somefilepath to "/Users/Design_005/Desktop/Start/TextFile.txt"
set somefolderpath to "/Users/Design_005/Desktop/End"
# Note that we use `as POSIX file` even in the case of the *folder*
# - this works, however.
tell application "Finder"
move somefilepath as POSIX file to somefolderpath as POSIX file
end tell
Note:
as POSIX file returns a file object in both cases, but Finder still handles the move properly - it is fine to use POSIX file with both files and folders.
Note that using the prefix form of POSIX file- e.g., POSIX file "/Library", only works with a path string literal; if you try to build the path string as an expression, it breaks (in the "Finder" context, but NOT in the AppleScript context(!)): POSIX file ("/" & "Library") - by contrast, "/" & "Library" as POSIX file works (in both contexts) - go figure. To be safe, always use the postfix form: ... as POSIX file
A downside of using as POSIX file - at least as of OS X 10.9 - is that the error messages are cryptic if a file/folder doesn't exist: you'll see Finder got an error: Handler can’t handle objects of this class. and Finder got an error: AppleEvent handler failed. - both with number -10000.
(Using folder directly with a POSIX path, as in an earlier version of the question - e.g., folder "/Library" - ONLY works in a "System Events" context, and is therefore NOT an option in the "Finder" context.)
As for what changed in AppleScript OS X 10.9:
The behavior you see appears to be a bug (also see #Jerry Stratton's answer); nothing in the AppleScript release notes for 10.9 indicates a change in that area.
I now think that the problem affects OS X 10.8 as well.
I encourage you to submit a bug report to Apple at http://bugreport.apple.com, as I already have.
Sadly, handling files, folders, and aliases in AppleScript has always been a mess, with confusion stemming from classes of the same name from different dictionaries (AppleScript itself, System Events, Finder) with subtly different behavior.
A general recommendation: for file-system manipulation, use the tell application "Finder" context.
The "System Events" dictionary, in its Disk-Folder-File Suite, duplicates some of Finder's file-system manipulation functionality, but only some - a curious omission is a file copy command, for instance.

AppleScript choose file or folder

can I use AppleScript to choose either file or folder in one time?
Now I could use
tell application "SystemUIServer" to return POSIX path of (choose file)
or
tell application "SystemUIServer" to return POSIX path of (choose folder)
to get file or folder. However I cannot get file or folder in one time.
No, you can't do it with "choose file" or "choose folder" verbs, but choosing a file or folder (or multiple files/folders) is supported by the underlying NSOpenPanel. So you can do it with AppleScriptObjC. Here's an example using ASObjCRunner (derived from here):
script chooseFilesOrFolders
tell current application's NSOpenPanel's openPanel()
setTitle_("Choose Files or Folders") -- window title, default is "Open"
setPrompt_("Choose") -- button name, default is "Open"
setCanChooseFiles_(true)
setCanChooseDirectories_(true)
setAllowsMultipleSelection_(true) -- remove if you only want a single file/folder
get its runModal() as integer -- show the panel
if result is current application's NSFileHandlingPanelCancelButton then error number -128 -- cancelled
return URLs() as list
end tell
end script
tell application "ASObjC Runner"
activate
run the script {chooseFilesOrFolders} with response
end tell
ASObjCRunner converts a NSArray of NSURL objects into an AppleScript list of files; the results can look something like:
{file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}
Firstly, you don't need a tell for that.
POSIX path of (choose file)
Secondly, it is not clear why you need this. Do you mean you want to select a file and it's folder? That's not how you do it; you select the file then parse the file path for the containing folder or use one of the many methods to do that, like
set f to (choose file)
set posixF to POSIX path of f
tell application "Finder" to set filesDir to container of f as alias as text
set posixDir to POSIX path of filesDir
{f, posixF, filesDir, posixDir}
If you want to be able to select multiple folders and files at the same time, I don't think there is a "pure applescript" way to do this (aside from using a drag-drop aware script application).

move a file in finder with applescript

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

Resources