I have a script that someone else wrote. It works on his computer but not mine.
Originally it said "as alias", but I kept getting an error message as follows:
"Can’t make file "Macintosh HD:Users:williamsato:Desktop:Photobooth:CurrentSession" into type alias."
So I changed alias to string, following a suggestion made in another post.
Now I don't get that error message, but I do get a new one as shown below:
"Can’t get every file of "Macintosh HD:Users:williamsato:Desktop:PhotoBooth:CurrentSession"."
And it highlights the line "move files of SourceFolder to DestinationFolder with replacing"
Not sure what is going wrong.
on run {input, parameters}
set SourceFolder to POSIX file "/Users:/williamsato/Desktop/Photobooth/CurrentSession" as string
set DestinationFolder to POSIX file "/Users/williamsato/Desktop/Photobooth/PreviousSessions" as string
tell application "Finder"
move files of SourceFolder to DestinationFolder with replacing
end tell
(* Clear Large Type *)
tell application "System Events" to keystroke "a" using command down
end run
Here are three different way of forming the SourceFolder and DestinationFolder variables and moving the files:
set SourceFolder to (path to desktop folder as string) & "Photobooth:CurrentSession:" as alias
set DestinationFolder to (path to desktop folder as string) & "Photobooth:PreviousSessions:" as alias
tell application "Finder" to move files of SourceFolder to DestinationFolder with replacing
set SourceFolder to alias "Macintosh HD:Users:williamsato:Desktop:Photobooth:CurrentSession:"
set DestinationFolder to alias "Macintosh HD:Users:williamsato:Desktop:Photobooth:PreviousSessions:"
tell application "Finder" to move files of SourceFolder to DestinationFolder with replacing
set SourceFolder to POSIX file "/Users/williamsato/Desktop/Photobooth/CurrentSession" as alias
set DestinationFolder to POSIX file "/Users/williamsato/Desktop/Photobooth/PreviousSessions" as alias
tell application "Finder" to move files of SourceFolder to DestinationFolder with replacing
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
Related
I can copy the file using AppleScript.
But how can I use it to paste the copied file to another folder?
I couldn't find the right operator or similar question.
tell application "Finder" to set the clipboard to (POSIX file "/Users/bgbg/Library/Messages/chat.db")
It definitely seems odd to me to use the clipboard as the variable for copying and moving files or folders and to use the keystroke command to paste a file into a folder. However, if you are hell-bent on doing things this way, I do believe this following AppleScript code will accomplish what you’re looking to achieve
-- Set The Clipboard To A File (This Only Works With A Single File¬
-- Or Folder And Not With A List Of Files Or Folders)
-- set the clipboard to POSIX path of (choose file with prompt ¬
-- "Set The Clipboard To A Single File Or Folder" invisibles false ¬
-- without multiple selections allowed) as POSIX file
set the clipboard to POSIX path of (((path to library folder from user domain as text) ¬
& "Messages:chat.db") as alias) as POSIX file
set destinationFolder to (choose folder with prompt ¬
"Choose Your Destinations Folder" with invisibles)
tell application "Finder"
activate
repeat until frontmost
delay 0.1
end repeat
reveal destinationFolder
end tell
delay 1
tell application "System Events" to keystroke "v" using {command down}
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
I'm having trouble getting AppleScript to read a list of files from a folder without receiving the error message "Can't get every file from folder XXX"
set targetfolder to ("DICTAPHONE:DSS_FLDA:")
tell application "Finder"
set fileselection to every file in targetfolder
endtell
This worked flawlessly before upgrading to Mavericks. Paths are correct. I tried it with a different folder on my startup disk and got the same result.
Try using folder targetfolder instead of targetfolder:
set targetfolder to ("Macintosh HD:Library:Desktop Pictures")
tell application "Finder"
files of (folder targetfolder)
end tell
Try this to get the information:
set targetfolder to (path to desktop as alias)
tell application "Finder"
set fileselection to get the name of every file of targetfolder
end tell
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"
I've been trying to get System Events to duplicate files in AppleScript and I've been failing :) I eventually always get the error "error "Files can not be copied." number -1717". So I changed my tactics and tried using the Finder to make sure what i was trying to do was correct. Here is the code that works:
tell application "System Events"
set desktopFolder to (path to desktop folder) as string
set fullPath to desktopFolder & "Temp Export From DO"
set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
set source to path of DOEntry
log "Source file: " & source
set destination to fullPath as string
log "Destination folder: " & destination
tell application "Finder"
duplicate file source to folder destination with replacing
end tell
end repeat
end tell
If I remove that last tell, so that it uses System Events, I get the same error noted above. The dictionary for System Events standard suite has a "duplicate" command so I'm not sure what is going on here. Also, "Learning AppleScript, 3rd ed" from APress notes:
"One particularly annoying omission in System Events is that it can’t yet duplicate files and folders; if you need to do this, the Finder is your best bet."
The 3rd edition is from 2010. It would seem that even in Mountain Lion this is still true. Can anyone confirm this? The 1717 error number lists everywhere else as a handler error and i'm not using handlers.
Unfortunately, you cannot duplicate files using System Events - you have to use the Finder. Even in the answer provided by adayzdone, System Events is not actually handling the duplication.
This looks like it's working (because it's inside a System Events tell block)...
tell application "System Events"
duplicate myFile to myFolder
end tell
...but if you inspect the event log you'll see that the Finder is actually performing the duplication. Behind the scenes, you are passing two Finder objects to System Events. System Events doesn't know how to handle Finder objects, so execution is passed to the objects' owner, the Finder, which executes the command.
For file duplication in AppleScript, you are unfortunately limited to using the Finder or the command line via do shell script.
Try:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
log "Source file: " & DOEntry
log "Destination folder: " & desktopFolder
tell application "Finder" to duplicate file DOEntry to desktopFolder with replacing
end repeat
If you don't need to log the values you can simply:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
tell application "Finder" to duplicate theDOEntries to desktopFolder with replacing
Or:
set desktopFolder to quoted form of (POSIX path of (path to desktop folder as text) & "Temp Export From DO")
do shell script "find '/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries' -name \"*.doentry\" -type f -print0 | xargs -0 -I {} cp -a {} " & desktopFolder
Getting back to your question, the duplicate commands creates duplicates of Finder items. You can use System Events to duplicate Finder items like this:
tell application "Finder"
set myFile to file ((path to desktop as text) & "Test File.txt")
set myFolder to folder ((path to desktop as text) & "Test Folder")
end tell
tell application "System Events"
duplicate myFile to myFolder
end tell