How to duplicate desktop file on applescript - macos

I'm trying to duplicate my desktop file on apple script using script editor but I can't figure out how to do it. Can someone please help me?

The following command will duplicate your Desktop folder in Finder as Desktop copy and will be in your Home folder:
tell application "Finder" to duplicate desktop
The following commands will duplicate the target object directly on the Desktop, i.e., objects within the Desktop folder in your Home folder in Finder .
The following command will duplicate every file on your Desktop:
tell application "Finder" to duplicate every file of desktop
The following command will duplicate every folder on your Desktop:
tell application "Finder" to duplicate every folder of (path to desktop)
The following command will duplicate every file and folder on your Desktop:
tell application "Finder" to duplicate every item of (path to desktop)
NOTES:
This is to address the comment made by has:
As to: "1. You don’t need path to desktop as Finder’s application object already has a desktop property containing a reference to the user’s Desktop."
This is not categorically a true statement and depends on how it's being used. It's true with the Desktop folder itself and files within the Desktop folder, however it is absolutely false with duplicate every folder of and duplicate every item of, and without using (path to desktop) in these instances it errors out with the following error statement:
"Finder got an error: The folder “” can’t be moved into itself." number -122
As to: "2. Never do duplicate every item of desktop – if the Finder’s preferences are set to display mounted volumes on the Desktop then those will be included too, and you definitely don’t want to duplicate those!"
That statement is categorically false!
He/she (has) obviously doesn't know what he/she is talking about because mounted volumes are not located within the Desktop folder in your Home folder. By default, they are mounted at /Volumes and while they can appear on the Desktop, they are not directly connected to the Desktop folder in your Home folder.
In this reference the Desktop and Desktop folder in your Home folder are not the same thing!

Related

applescript to delete files in applications support directory

As part of my uninstaller script, I'd like to delete my app's directory in ~/Application Support/My App Name.
My uninstaller script is Apple Script. I've tried the following:
tell application "Finder" to delete (POSIX file "~/Library/Application Support/My App Name")
do shell script \"
sudo rm -rf '~/Library/Application Support/My App Name'
\" with administrator privileges
"""
But none of these had any effect, my app's Application Support directory remains. How can I get this done?
If you use System Events instead of Finder for file system operations, then you can freely use posix-style paths, including the tilde. Then your script needs very little changing:
tell application "System Events" to delete the item ¬
"~/Library/Application Support/My App Name"
However, the System Events delete command mirrors the shell rm command, in that it permanently deletes files from the filesystem, without sending them to the trash.
Instead of supplying a hard-coded path to the Application Support folder, you can utilise the System Events property of the same name:
tell application "System Events" to delete the folder ¬
"My App Name" in the application support folder
Tip: There are one or two Finder-specific capabilities that can only be achieved using Finder, namely accessing Finder windows, getting or setting selected files in Finder (by way of the selection property or the select command), revealing files (using the reveal command), and getting the insertion location property. But other than when you need to invoke one of these, it's best to avoid Finder as it's slow, buggy (so is System Events, but not as much), it doesn't handle posix-style paths, and it blocks Finder during any and all operations (and, since it's slow, it blocks for a potentially long time, or times-out altogether meaning you have to restart Finder). Even when invoking one of the Finder-specific functions, I typically have it perform literally that one task, making sure it returns either alias class file referneces, or plain text file paths (HFS-style colon-delimited paths are fine), rather than Finder file references. System Events can utilise alias references as well as HFS-style file paths.
The main issue is that the tilde is not expanded.
In the user folder you don't need administrator privileges. Just get the folder with a relative path.
set applicationSupportFolder to path to application support folder from user domain
tell application "Finder" to delete folder "My App Name" of applicationSupportFolder

How do I Copy files to a USB with apple script that runs on said USB?

I run a computer lab at Denver University. We are having an issue with our backup system, and I need to copy/duplicate all of the files on the (Apple) computers onto a USB that I keep at my desk.
At the moment, I need to plug in a USB into each of the computers and manually duplicate each of the items of the "documents" folder and deposit them into a new folder on my USB. I need an Apple Script that I put on my USB, insert my USB into a computer, activate the app, and it will make a new directory named "(User name) upload", and deposit all of the items into that directory. Here is what I have so far:
tell application "Finder"
set theFolder to disk / Volumes / Lexar / stuff
set Files1 to Users / matanya / documents
tell application "Finder"
try
duplicate file Files1 to theFolder
on error
tell application "Finder"
display dialog "Transfer Failed"
end tell
end try
end tell
one of the issues is that every time I run the script, I get an error that says the the variable "Volumes" is not defined. Another one is that I am afraid that when I plug this script into another computer, it will not find the folder "matanya" that I have in my directory. Is there a way to call it "home" or something?
First of all the Finder accepts only HFS paths – starting with a disk name and colon separated. The folder Volumes doesn't matter when using HFS paths.
path to documents folder points always to the folder Documents of the current user.
set theFolder to "Lexar:stuff:"
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
try
duplicate documentsFolder to folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
More sophisticated this version creates a folder with the short user name of the current user and duplicates the documents folder to that individual folder
set theFolder to "Lexar:stuff:"
set currentUser to short user name of (system info)
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
if not (exists folder currentUser of folder theFolder) then
make new folder at folder theFolder with properties {name:currentUser}
end if
try
duplicate documentsFolder to folder currentUser of folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
With launchd you even could run this script automatically whenever the USB drive is inserted.

Transfer Music file from USB into "Automatically Add to iTunes" and play it with Applescript

I need a script that moves a music file from my USB Music folder into the "Automatically Add to iTunes" folder, and then plays that file. This is what I have so far:
tell application "Finder"
move items from "LEXAR:Pictures:Music:" to "Macintosh HD:User:" --this is where i'm lost, i need the path that doesnt include my name as the User
--and i need some way to tell itunes to play that file
sorry about the weird code setup. What I want to happen is I insert my USB into someone's computer, click on this script as an .app, and it will transfer the files in my USB under "Music" into the "Automatically Add to iTunes" and then launching iTunes, playing that music file.
You can use this:
tell application "Finder"
set userPath to (path to home folder as text)
set directoryPath to "Music:iTunes:iTunes Media:Automatically Add to iTunes"
set fullPath to userPath & directoryPath
move every file of folder "LEXAR:Pictures:Music:" to folder fullPath
end tell
and then use that variable in order to get to the home folder of the currently logged in user. From there, you can reference the Automatically Add to iTunes folder :)

moving one folder to another folder - applescript

I'm trying to move a user-selected folder to another folder, but I don't get it: whenever I look it up, it looks too complicated, even though I know it's not supposed to be.
Here's the code I have so far - please help:
choose folder with prompt "Select folder:"
on open of finderObject
tell application "Finder" to move (finderObject) to folder "Library:Application Support"
end open
end
First off, you have to assign the chosen folder to a variable.
Also, many special folders in the OSX file structure have special names to get their paths, including the library and the application support folders. So, your script can simply be:
set finderObject to choose folder with prompt "Select folder:"
tell app "Finder" to move finderObject to folder (path to Application Support folder from local domain as string)
However, that is the root level Library folder. I suspect you will want to use the Application Support folder in the ~/ user domain. For that, change the "from local domain" to "from user domain".

Move folder to external drive via applescript

I need to MOVE (not copy) every file (including subfolders) in "Downloads" to an external drive called "Drive" in the location "Volumes/Drive/Apple/MacBackup/Downloads". I have tried countless times and it is just not working... Here is my code right now:
tell application "Finder" to move entire contents of folder "Users:myUsername:Downloads" to folder "Drive:Apple:MacBackup:Downloads"
I am getting the error:
Finder got an error: Can’t get folder "Users:myUsername:Downloads".
HFS paths have to begin with the name of the disk, like
"Macintosh HD:Users:myUsername:Downloads"
You have probably taken it from a POSIX path, were the first slash represents the startup volume.
To be sure just run
choose folder
and copy the path from the result.
The error is because that is not a correctly formed path. You should use the special folder designation (path to downloads folder).
tell application "Finder" to move entire contents of (path to downloads folder) to folder "Drive:Apple:MacBackup:Downloads"
Also, I would strongly encourage you to copy the files, then delete the old files. Moving files is asking for trouble if something happens mid-process, and it's lost in transit. (think star trek transporter problem)

Resources