applescript move folder script - applescript

I am a complete applescript virgin and I have 2 versions of the same application that use different frameworks that have the same name.
I'm looking to create 2 scripts that move the correct version of the framework into the frameworks folder so I can swap them out quickly from the desktop.
This is what I have so far but it is throwing up an AppleEvent Handler failed -10000 error.
tell application "Finder"
duplicate POSIX file "Machintosh HD/Framework Store/Test.framework" to POSIX file "Machintosh HD/Library/Frameworks/Test.framework" with replacing
end tell
Can someone point out where I am going wrong?
Any help is greatly appreciated.

Just noticed you also had the Had disk name. The first forward slash "/" is all you need to refer to that.
tell application "Finder"
set rootFolder to POSIX file "/Framework Store/Test.framework"
set targetFolder to POSIX file "/Library/Frameworks/"
duplicate rootFolder to targetFolder with replacing
end tell

Try:
tell application "Finder"
duplicate POSIX file "Machintosh HD/Framework Store/Test.framework" to POSIX file "Machintosh HD/Library/Frameworks" with replacing
end tell

Related

find fileName in a folder using apple script

i would like to get the first file name of a folder using appleScript language
it should look like that:
Folder: photo.png, photo1.png, photo2.png, photo3.png
tell application "Finder"
set fileName to (1° file in this case photo.png)
end tell
please anyone than can help me out thanks
Almost.
tell application "Finder"
set fileName to name of first file of folder "Macintosh HD:path:to:folder"
end tell

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.

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 Error: Finder got an error: Can’t get folder "~/Library/Services"

Here is my code:
tell application "Finder"
move POSIX file "/Volumes/Toggle Desktop Icons/Toggle Desktop Icons/Install Files/Hide Icons.workflow" to folder "~/Library/Services"
end tell
And it always gets this error: Can’t get folder "~/Library/Services".
Any help? This also needs to work on any mac I run the code on. Without changing the code
Thanks.
There is a system property path to home folder that you could use. And for simplicity sake use the native mac HFS path delimiter ":" to reference the rest of your path.
Try this (adding back in your full source path),
tell application "Finder"
move POSIX file "/Volumes/.../Hide Icons.workflow" to folder (((path to home folder) as text) & "Library:Services")
end tell

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