moving one folder to another folder - applescript - macos

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".

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 to detect and manipulate files on a USB using AppleScript

I have recently started a little project of mine using languages such as Python or AppleScript to manipulate files and other things on a Mac. I am making multiple different programs that do these things.
Recently I began to write a program that changes the desktop background to a file on the USB stick that the program is located on. It is intended to work across different computers, so I have to keep it on the USB stick.
The issue is that when I run the program from my computer, the AppleScript seems to not be able to find the files on the USB. I am also not sure whether or not I will need to change the code when I move the program onto the USB.
The program works fine when I use files on the computer itself, but it seems to come up with an error every time it tries to retrieve an image from the USB.
Here is the appropriated code.
set Username to system attribute "USER"
set Photo to (random number from 1 to 5) as text
tell application "Finder"
make new folder at desktop with properties {name:"DesktopFolder"}
move entire contents of folder "USB/Photos/" to folder "Users/" & Username & "/Desktop/DesktopFolder/"
set desktop picture to POSIX file "/Users/" & Username & "/Desktop/DesktopFolder/Image" & Photo & ".png"
end tell
Of this, the line move entire contents of folder "USB/Photos/" to folder "Users/" & Username & "/Desktop/DesktopFolder/" is the one producing the following error Finder got an error: Can’t get folder "USB/Photos/".
What I would like to happen is that the images from the USB are copied into the new folder on the desktop, and then a random photo from this folder is chosen as the new desktop background.
Any help would be appreciated. Thanks!
The main issue is that Finder doesn't understand posix paths (i.e. slash notation); it operates using HFS paths, which uses colons in place of slashes, and substitutes in the disk name at the beginning of the file path.
So, the equivalent HFS path that points to the same location as the posix path
/Users/{{user}}/Desktop/DesktopFolder/Image4.png
is written as
Macintosh HD:{{user}}:Desktop:DesktopFolder:Image4.png
Therefore, to point to a folder on your USB drive named "USB", you would write:
USB:Photos:
Here's a modified and corrected/improved edit of your script:
set n to (random number from 1 to 5) as text
tell application "Finder"
set F to make new folder at desktop with properties {name:"DesktopFolder"}
move every item of folder "USB:Photos:" to F
set desktop picture to the file ("Image" & n & ".png") in F
end tell
I've done away with a lot of your path references that were expressed explicitly as broken string fragments, and instead set a variable to store a reference to the location of the folder you make on your desktop. That way, you can use the variable reference to target that folder for moving and selecting files.

How to duplicate desktop file on applescript

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!

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)

Renaming Files Sequentially as they are added to a folder using Automator or Applescript

I need to rename image files sequentially as they are added to a folder. ie. image-0001.jpg and image-0002.jpg are in a folder I add test.jpg and it is renamed image-0003.jpg. I have tried automators rename function but it will start over with image-0001.jpg each time a new file is added instead of continuing the sequence.
Any help is greatly appreciated.
You could make this easy on yourself by using a handy application built into the OS called "Folder Actions". Folder Actions contains one or more special handlers, formally known as folder action event handlers, that run when they are triggered by a change in the target folder. I know I'm confusing but I'll do my best.
What you are trying to accomplish requires an adding folder items to event handler. It requires one direct parameter, which can be anything you wish i.e. target_folder. The handler requires an additional parameter as well; after receiving, which should also be a variable name, i.e. these_items. I have composed a script for you that should do the trick. I have added comments that show you what I'm doing when I do it. Here it is:
on adding folder items to the target_folder after receiving these_items
tell application "Finder"
set all_images to every item of the target_folder as list
repeat with i from 1 to the count of these_items --iterates through all the items you dropped in the folder
set this_image to item i of these_items --the current image
set the name of this_image to "image" & (i + the count of all_images) as string --renames the image based on the number of images already in the folder
end repeat
end tell
end adding folder items to
YAY! The script is done! But are WE done? Not quite. We still need to attach the script to a folder (the script won't run if you try to execute it in script editor).
To do this, first save the script as a Script File in the Folder Action Scripts folder in the Scripts folder in either the local Library folder or the current user's Library folder. Create the folder yourself if it doesn't already exist. Next, launch the Folder Actions Setup application by double-clicking it in the AppleScript folder in the Applications folder. In the window that comes up, click the + button under the table on the left (click the "Enable Folder Actions" checkbox if it isn't already checked) to open a standard file browser sheet, navigate to your desired folder, and click "Open". The Choose a Script to Attach sheet automatically opens, listing all the scripts in all of the Folder Action Script folders. Choose the newly-created script, click "Attach", and BAM you are done!
To see the script in action, drag an image onto the folder. The image is instantly renamed, regardless of if the folder window is open. If you have any questions, or if the script doesn't work, just ask me. :)
well without digging through some code and handin you an answer I'll tell you want you want to do
is create a while loop that checks for the existence of image-000 & i where i is a variable of course and if it exists increment i then when the file doesn't exist rename your file.

Resources