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
Related
I am trying to make an Applescript that will launch Photoshop and call the Automate Batch function to run a specific Action. I have zero experience doing this and have only gotten snippets of code from my searching. I was wondering if someone could help me on this.. in particular if it is at all possible to pass a source folder to the batch call and then how to make the Batch call.
In particular, I am having issues trying to figure out how to:
Pass the source folder into the Batch Options
Call a specific Action in the Batch_Options from my Photoshop
Run the batch call with these options
I've updated with the latest code that is partially there...
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "Save as Photoshop PDF"
activate
try
set Batch_Options to {class:batch options, destination:save and close, error file:Error_File, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
batch "Save" from files folderList from action_set_name with options Batch_Options
end try
end tell
output:
"File Names are: Macintosh HD:Users:Joe:Desktop:Temp:Creature01_CO_v003.psdMacintosh HD:Users:Joe:Desktop:Temp:SecretLaboratory.psd"
Photoshop opens, and then nothing happens...
The following seems to work in the Automator:
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
#Alias list needs to be converted to a string for it to work in Photoshop batch call
set FileNames to {}
repeat with i from 1 to the count of folderList
set current_file to item i of folderList
copy current_file as text to the end of FileNames
end repeat
#Setup error log
set err_log to (sourceFolder as string) & "Error_Log.txt"
if not (exists file err_log) then
make new file at sourceFolder with properties {name:"Error_Log.txt"}
end if
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "SetA"
activate
set Batch_Options to {class:batch options, destination:save and close, error file:err_log, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
#First arg is the Action Name, second is the list of files, third is the Action Set Name
batch "ActionA" from files FileNames from "SetA" with options Batch_Options
end tell
could anyone tell me how I can create a simple installer of Kexts using AppleScrit in Xcode?
I've tried the following:
on installKext:sender
choose file
copy the result to thisFile
copy the (info for thisFile) to fileInfo
copy the name extension of the fileInfo to nameExtension
copy the file type of the fileInfo to filetype
tell application "Finder"
if the filetype is "Kext" or ¬
the nameExtension is "Kext" then
move thisFile to folder "Extensions" of folder "Library" of folder "System" of startup disk
end if
end tell
end installKext:
I Have this errors:
installKext:]: Not authorized to send Apple events to Finder. (error
-1743)
Error Domain=PlugInKit Code=13 "query cancelled"
UserInfo={NSLocalizedDescription=query cancelled}
You cannot move files with the Finder into folders which belong to the system.
You need root privileges for example with do shell script .... with administrator privileges
on installKext:sender
set theFile to choose file
tell application "System Events"
set {name extension:nameExtension, file type:fileType} to theFile
end tell
if fileType is "Kext" or nameExtension is "Kext" then
do shell script "mv " & quoted form of POSIX path of theFile & space & "/System/Library/Extensions/" with administrator privileges
end if
end installKext:
You must add the following in info.plist:
now everything works like a charm.
With the help of some users on this form I was able to create this script below which lets you choose a folder then choose which folders within the folder you want to rsync into a OneDrive Backup Folder.
Basically were migrating from a on site network storage to OneDrive for Business and want to create a script that as easy as possible for our users.
The issue I have is I dont want the users to be able to choose the original folder, I want to set the variable up front.
In the script I use:
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?
set OuserName to do shell script "whoami"
set userName to do shell script "echo " & OuserName & " | tr a-z A-Z"
tell application "Finder"
if not (disk userName exists) then
mount volume "SMB Server/" & userName & "$"
end if
delay 2
set theDialogText to "
- Mac H-Drive Migration Tool -
This Application Will Migrate a Copy of Your H Drive Data
to your OneDrive for Buisness Folder Locally on Your Mac
Migration Backup Location:
/Users/" & OuserName & "/OneDrive Folder/H-Drive Migration Backup
** Important **
In the Next Window Please Choose
The Root Folder of Your H Drive
The Drive Label Should Look Like: " & userName & "$"
display dialog theDialogText
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
do shell script "mkdir -p ~/'OneDrive Folder'/'H-Drive Migration Backup'"
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
set AppName to "OneDrive.app"
tell application "Finder" to set Answer_ to exists application file ((path to applications folder as string) & AppName)
if Answer_ is false then
beep
beep
beep
beep
beep
end if
delay 1.5
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
end tell
repeat with thisName in theChosenNames
tell application "Terminal"
do script ("rsync -avpz --delete " & (quoted form of POSIX path of ((theFolder as text) & thisName)) & space & (quoted form of POSIX path of HDriveBackupFolder)
end tell
end repeat
end tell
You asked:
"How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?"
Also, just prior to that, you said:
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
First, lets address setting the value of the theFolder variable. It should be as follows:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
I assume the error is being thrown at the following point in the code:
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
Change set theFolderNames to name of folders of theFolder to:
set theFolderNames to name of folders of container theFolder
To test this, I mounted a volume named MYERSMI5$ at /Volumes/ and created some folders within /Volumes/MYERSMI5$/. Then running the following code:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
tell application "Finder"
activate
set theFolderNames to name of folders of container theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
end tell
It produced a list box, containing the names of the folders I created at that location, to choose from.
I did not try to run the entire block of code you included in your question, so if you're having other issues, you'll need to follow up after making the changes mentioned in my answer. There are good reasons why questions involving debugging code should conform to How to create a Minimal, Complete,and Verifiable example.
You may also want to review the Variables and Properties section in the AppleScript Language Guide.
I have created an Applescript that will, with one click, add the contents of a specific folder on my hard drive to iTunes (I really want to avoid using the iTunes organisational system).
Applescript
tell application "iTunes"
set theFolder to ("Macintosh HD:Temp to be Listened to:Temp on iPod:")
set with_subfolders to true --list subfolders or not (recursion)
set parent_folder to theFolder
end tell
tell application "System Events"
set item_list to parent_folder
end tell
tell application "iTunes"
add contents of parent_folder to user playlist "Temp on iPod"
end tell
However, it only imports into iTunes the files from the top level/parent folder.
I want to include files from the folders in the parent folder.
Is there a way I can get it to be recursive?
You do not need to get all files from a folder, since iTunes does this automatically and recursively from a folder.
Just add the parent folder, like this:
set parent_folder to "Macintosh HD:Temp to be Listened to:Temp on iPod:" as alias
tell application "iTunes"
add parent_folder to user playlist "Temp on iPod"
end tell
Solution #1: Using entire contents of the Finder - not very fast
set theFolder to "Macintosh HD:Temp to be Listened to:Temp on iPod:"
tell application "Finder" to set filesToAdd to files of entire contents of folder theFolder as alias list
tell application "iTunes" to add filesToAdd to user playlist "Temp on iPod"
Solution #2 : Using Spotlight - very fast, but the volume must be indexed.
set theFolder to "/Temp to be Listened to/Temp on iPod"
set fileList to paragraphs of (do shell script "mdfind -onlyin " & quoted form of theFolder & space & quoted form of "kMDItemContentTypeTree == '*public.audio*'")
set filesToAdd to {}
repeat with aFile in fileList
set end of filesToAdd to POSIX file aFile as alias
end repeat
tell application "iTunes" to add filesToAdd to user playlist "Temp on iPod"
Yet again flummoxed by syntax and ordering of an applescript workflow solution
When I create the following code
tell application "Finder"
set remote_p to alias (POSIX file '/Volumes/WAM XSAN/Audio/AAA)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
Everything works fine. However, I need to create the main_folder in different locations dependent on the input "client_code" and "department", So i tried this:
tell application "Finder"
set x_san to "/Volumes/WAM XSAN/"
set x_sannewpath to (x_san & department & "/" & client_code)
set x_sanfolder to POSIX file x_sannewpath
set remote_p to alias (POSIX file x_sanfolder)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
And the error comes back "Can't get "Volumes/WAM XSAN/Audio/AAA" of Application Finder"
Where am i going wrong in setting up the POSIX paths?
Please help!!!!
Try this example. I tried to model it after what you're trying to do. Note that I have moved most of the code outside the Finder tell block of code. The Finder does not know the "posix file" command. That's an applescript command, not a Finder command. Also you don't need the Finder to set variables and add strings together. We can do all of that outside the Finder.
Even though it sometimes works when you have the "posix file" command inside a Finder tell block of code, it's always best if you only tell applications to do what they know how to do. You can find what an application knows how to do by looking in its applescript dictionary. Check the Finder dictionary and you will see that "posix file" is not one of its commands.
Anyway, this will create a folder on your desktop. I hope this helps.
set x_san to "/Users/hmcshane/"
set client_code to "Desktop"
set temp_name to "test folder"
set x_sannewpath to x_san & client_code
set applescript_x_sannewpath to POSIX file x_sannewpath
tell application "Finder"
set main_folder to (make new folder at applescript_x_sannewpath with properties {name:temp_name}) as alias
end tell
Try:
set department to "Audio"
set client_code to "AAA"
set temp_name to "New Folder"
set x_san to "Volumes/WAM XSAN/"
set x_sannewpath to x_san & department & "/" & client_code
tell application "Finder" to set main_folder to (make new folder at POSIX file x_sannewpath with properties {name:temp_name})