Applescript error "invalid key form" when run - applescript

I tried to run this applescript:
tell application "Finder"
duplicate POSIX file contents "/Users/xx/Desktop/xxxx/" to POSIX file "/USB/" with replacing
delete POSIX file contents "/Users/felix/Desktop/xxxx/"
empty trash
end tell
but every time an error message appears saying:
Invalid key form.

You don't need to use contents. This should work.
tell application "Finder"
set src to POSIX file "/Users/xx/Desktop/test.tmp"
set dst to POSIX file "/USB/"
duplicate src to dst with replacing
end tell

Related

AppleScript - Copy All files/folders from source folder to destination folder

I'm trying to write an AppleScript that will simply copy the contents (both folders and files) from a specified source folder to a specified destination folder. At the moment my script runs but only copies one file and I can't work out how to get it to copy all files in the folder.
Here's my script:
set sourceFolder to (POSIX file "/Users/benny/Desktop/Projects/Source/Project1") as alias
set destinationFolder to (POSIX file "/Users/benny/Documents/Masters/Project1") as alias
tell application "System Events"
set availableSourceFiles to every file of sourceFolder whose visible is true
set filesOfTargetFolder to files of destinationFolder whose visible is true
end tell
-- if no more source file is available, quit this script
if (count of availableSourceFiles) = 0 then
quit
end if
set sourceFile to (first item of availableSourceFiles) as alias
-- use the Finder to copy the file
tell application "Finder"
-- duplicate the file to the target folder
duplicate sourceFile to destinationFolder
end tell
I'm assuming I need to include a for each type loop but can't get the syntax correct here. Haven't written AppleScripts in many years so trying to remember how it all works.
If the destination "Project1" folder doesn't have stuff in it already, then duplicating the folder is likely to be quicker:
tell application id "com.apple.Finder" to duplicate folder POSIX file ¬
"/Users/benny/Desktop/Projects/Source/Project1" to the folder ¬
POSIX file "/Users/benny/Documents/Masters" with replacing
However, if that's not an option, then I'd stick with your method and copy the contents of the folder across instead:
set here to POSIX file "/Users/benny/Desktop/Projects/Source/Project1"
set there to POSIX file "/Users/benny/Documents/Masters"
tell application id "com.apple.Finder" to duplicate ¬
every item in the folder here to there
Bear in mind that if there's a file or folder at any of the destinations that intended to be occupied by one of the incoming source items, Finder will throw an error. You would typically incorporate some sort of check ahead of the copy.

Using AppleScript to Select a File

I'm trying to get AppleScript to select a file, but I'm getting an error when I execute the script.
Here's the code
tell application "System Events"
set a to "/Users/me/files/"
set fileName to "myFile.jpg"
set thePath to POSIX path of a
tell application "Finder"
set selection to fileName of thePath
end tell
keystroke "c" using command down
end tell
I'm getting an error "Can’t get POSIX path of "/Users/me/files/"
Essentially, what I'm trying to do is find a way to select a file so that I can copy it for later. But I want to copy the actual file, not the path of the file. The idea is to create a service that copies the file so that I can paste it into another application easily.
If there's a better way to do this, then please let me know
These following 2 lines of code would copy your file to the clipboard. This would only work with a single file. Not multiple items.
activate
set the clipboard to POSIX file (POSIX path of (choose file))

POSIX file works in tell block

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.

POSIX File Error - Extraneous double quotes?

This applescript works
set myFile to (POSIX path "/Users/fred/Documents/data.dat")
This applescript doesn't work
set myFileName to "/Users/fred/Documents/data.dat"
set myFile to (POSIX path myFileName)
It fails with the error
get POSIX file "/Users/fred/Documents/data.dat"
--> error number -1728
Result:
error "iTunes got an error: Can’t get POSIX file \"/Users/fred/Documents/data.dat\"." number -1728 from file "Macintosh HD:Users:drew:Desktop:Music:DIY:DIY-01.mp3"
It looks as if when using the variable, POSIX path is including the double quotes as explicit characters in the file name. What am I doing wrong?
The script below reproduces the problem.
tell application "Finder"
set newFileName to "/Users"
set newFile to POSIX file newFileName
end tell
Thanks
OK - I've found out what I should be doing.
The script below works - you just need to coerce the variable rather than pass it to POSIX file
tell application "Finder"
set newFileName to "/Users"
set newFile to (newFileName as POSIX file)
end tell
Result...
file "Macintosh HD:Users"
Thanks for your assistance.
Andrew
"/Users/fred/Documents/data.dat" Is already a posix path
tell application "Finder" to open POSIX file "/Users/fred/Documents/data.dat"
or
tell application "System Events" to open "/Users/fred/Documents/data.dat"
Here is an example for iTunes:
tell application "Finder" to set myFile to (POSIX file "/Users/John/Desktop/08 5150.mp3")
tell application "iTunes" to set resultTrack to add myFile to playlist "test"

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