Apple Script: Copy file (being selected from prompt) to another location - macos

I am struggling with my Apple Script. I am asking to select a file and the selected file needs to be copied to another location. I am new to Apple Scripting, so probably made some mistake. I tried different versions with "copy" instead of "duplicate" or "alias" instead of "file", but nothing worked so far. Hope, somebody can help me figure this out.
This is what I scripted so far (I get an AppleEvent timed out):
set DefaultPath to POSIX file "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Test"
set DestFolder to "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Destination"
set theFile to (choose file with prompt "Select file:" default location (DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell

The problem is that the Finder doesn't support POSIX paths.
I recommend to use a relative path path to library folder from user domain and HFS paths (colon separated)
To satisfy the location parameter of choose file put the alias keyword in front of the (HFS) path
set cloudDocs to (path to library folder from user domain as text) & "Mobile Documents:com~apple~CloudDocs:"
set DefaultPath to cloudDocs & "FOLDER:Test:"
set DestFolder to cloudDocs & "FOLDER:Destination:"
set theFile to (choose file with prompt "Select file:" default location (alias DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell

Related

Applescript can't set name of file

So I'm trying to rename a file using Applescript. I searched it up and apparently the command is set name of file theFile to theString. I tried it and it didn't work. I got the error -10006, which (according to https://www.osstatus.com) is either errAEWriteDenied, errOSACantAssign or telCAUnavail, but I think it's probably the first one. Before you ask, I am the administrator of my machine and path to desktop links to the own Desktop to which I 100% have access. I don't know if that matters (it really shouldn't), but my Desktop is stored in my iCloud.
set thePath to the POSIX path of (path to desktop)
set theName to "hello world.txt"
set theFile to thePath & theName
set the name of file theFile to "hello.txt"
Only the Finder or System Events is able to set the name. In Finder it’s pretty easy because the desktop of the current user is the root folder.
tell application "Finder"
set theFile to "hello world.txt"
set theName to "hello"
set the name of file theFile to theName & ".txt"
end tell
By the way, the Finder doesn’t know about POSIX paths, it prefers HFS paths (colon separated)

How to Create a Simple kext Installer with AppleScript in Xcode?

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.

Applescript: Need Help Resolving - Can't Get every file of said folder?

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.

applescript Posix chaos

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})

does System Events in AppleScript support duplicating files?

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

Resources