AppleScript duplicate folder inside pre-existing folder - macos

I'm wondering if there is a way using ApplesScript to duplicate a folder, and all the files/folders inside to another location - while leaving any files that exist inside the location intact?

tell application "Finder" to duplicate folder "Macintosh HD:Users:YourCompName:Desktop:Test1" to "Macintosh HD:Users:YourCompName:Desktop:Test2"

Related

Applescript to move incoming pictures to other folder with delay

I'm an applescript noob. So I don't know anything about this type of coding.
I want to make a script for a watch folder. I work with an image program that processes images. The process takes a second to create the jpg. So the folder has to wait for a few seconds to move the image to another file.
So this is what I'm looking for: Empty folder - jpg in the folder - wait 2 sec - move jpg to another folder - empty folder.
Thank you!
This AppleScript will accomplish what you asked. By the very nature of AppleScript, reading it should be very self explanatory as to what each line does:
on adding folder items to ThisFolder after receiving SomeFiles
set ThatFolder to POSIX file "/path/to/new/folder"
delay 2
repeat with TheFile in SomeFiles
tell application "Finder" to ¬
if name extension of (TheFile as alias) is in {"JPG", "JPEG"} then ¬
move TheFile to ThatFolder
end repeat
end adding folder items to
This is designed to run as a folder action, which makes the folder you choose automatically monitored by MacOS so that, whenever it detects a change to that folder, it executes the script attached to it. In this instance—as you can infer from the first line of the script—this will automatically run each time a file is added to the folder in question.
To set up a folder action:
Copy the AppleScript above into Script Editor. Edit the second line to replace "/path/to/new/folder" with the path to the new folder into which you want your jpegs to be moved (keep the quotes). I wouldn't bother trying to run the script from inside the editor—it won't work. The path you type out must be in full, i.e. "/Users/Richard/Pictures/Processed" and not "~/Pictures/Processed".
Save it as whatever you like. However, it must be saved in the following directory: ~/Library/Scripts/Folder Action Scripts where ~ indicates your Home folder (i.e. /Users/Richard/ or whatever it is). If the folder "Folder Action Scripts" doesn't exist, create it.
Close Script Editor. Navigate to the folder that is going to be watched, i.e. the folder that your images will be waiting initially. Now navigate one level up, into the directory containing said folder.
Right-click on the folder and hover over the Services menu item at the bottom. Then select Folder Actions Setup...
Enable folder actions by checking the box at the top. If you folder doesn't already appear in the left-hand list, you can add it. Then, in the right-hand list, click the '+' at the bottom and the script you just saved should be one of many in the list of scripts that pop up. Select it and add it.
Make sure the check boxes next to your watched folder and your chosen script are both checked, and you're done.
Now, whenever the folder receives any files whatsoever, that script is executed. The script will move any files with extensions .jpg or .jpeg into your new folder. Just make sure the new folder already exists (I didn't incorporate a line to create it if it doesn't; the script will just throw an error and your image won't be moved).
Here is a little sample script which you could save on your Desktop as monitor:
#!/bin/bash
# Source directory to watch and destination directory to copy to
SRC="$HOME/Desktop/source"
DST="$HOME/Desktop/dest"
# Create directories if not existent
mkdir -p "$SRC" "$DST"
while : ; do
find "$SRC" -type f -iname "*.jpg" -Btime +2s -exec mv {} "$DST" \;
sleep 5
done
It basically watches a directory called Desktop/source and looks for any files in there whose name ends in "JPG" and which have not been modified in the last 2 seconds. If it finds any, it then moves them to directory called Desktop/dest and sleeps for 5 seconds before checking again.
You would need to start Terminal and make the script executable by typing:
chmod +x $HOME/Desktop/monitor
Then, whenever you want it running, you just double-click on the icon of monitor on your Desktop.

Delete most recently added directory to a directory in applescript

I'm making a script to delete messages history on a my mac. I need it to delete the most recently added folder to a directory. I can delete a folder that I have the name of, but I have no idea how to get the most recently added folder to delete it. The archive folder is ~/Library/Containers/com.apple.iChat/Data/Library/Messages/Archive.
Thanks
Sort the folders in Finder by creation date and get the last item
set archivedMessages to (path to library folder from user domain as text) & "Containers:com.apple.iChat:Data:Library:Messages:Archive:"
tell application "Finder"
set mostRecentChat to last item of (sort folders of folder archivedMessages by creation date)
delete mostRecentChat
end

Script to create a new directory filled with duplicate of specific files

I'm doing some repetitive work that involves creating a directory and duplicating a set of files into it. What i'm looking for is a script that will automate that process for me, which I can put into an Alfred workflow and just call up every time I need it.
I've gotten so far as a script that will create the directory in my front most Finder window, but I want that directory to contain duplicates of 4 specific files.
d="$(osascript -e 'tell app "Finder"
POSIX path of (insertion location as alias)
end')/new folder"
mkdir "$d"
cp /path/to/files/* "$d"
open -R "$d"
Or using AppleScript:
tell application "Finder"
set d to make new folder at (get insertion location) with properties {name:"new folder"}
duplicate files of (POSIX file "/path/to/files/" as alias) to d
reveal d
end tell

error "Finder got an error: The operation can’t be completed because there is already an item with that name." number -48

At work, I want to backup some Mac files to a Windows share and have created an AppleScript. It mounts the destination then creates a folder if it doesn't already exist. It then copies the contents of a local folder to this new folder on the destination. It then unmounts the destination
mount volume "smb://service.backup:<password>#server.domain.com/computer-backup"
set dest to result as alias
tell application "Finder"
if not (exists POSIX file "/Volumes/server.domain.com/computer-backup/Web") then make new folder with properties {name:"Web"} at "computer-backup"
duplicate items of folder "Macintosh HD:Library:Server:Web" to POSIX file "/Volumes/computer-backup/Web" with replacing
eject dest
end tell
The mount is fine. But if the folder "Web" exists on the destination then it errors - despite the "if not (exists" statement. I have a very similar script at home (with different usernames, passwords and server addresses) which works fine. I am pretty sure I have had this working at work as well (hence the use of POSIX) but not anymore.
I chose this route as a more granular alternative to TimeMachine and to show my boss I could write AppleScript :>)
Any help gratefully received.
All the best
John
Have you checked your 'Volumes' path? Path names usually does'nt contain the server name. I would go for (if not (exists POSIX file "/Volumes/computer-backup/Web") then make new folder with properties {name:"Web"} at "computer-backup") – tompaman Dec 27 '13 at 13:35

Applescript to duplicate file from mounted volume to desktop

I am just getting starteded with Applescript and need some help with a simple file copy from a Windows server. I am mounting a volume to a Windows share and I need to copy the files from that folder to the desktop. Here's the AppleScript I have so far.
tell application "Finder"
mount volume "smb://username:password#server.domain.com/source/MAC/StandardApps"
duplicate file "SymantecEndpointProtection.zip" of folder "StandardApps" to folder "Desktop"
end tell
The error I get is "Can't set folder \"Desktop\" to folder \"StandardApps\"."
I'm sure I'm missing something simple, again I'm new to Applescript. Grateful for any help. I will provide any additional info you need.
Thanks in advance.
You need to use the complete file paths. For example, the path to the desktop folder in your user's account can be found by using path to desktop folder. Another easy way to see what a file path is would be to look at the result from a choose file dialog.
I was able to get it working by just mounting to the source share and changing the duplicate line to:
duplicate file "SymantecEndPointProtection.zip" of folder "StandardApps" of folder "Mac" of disk "source" to folder desktopPath

Resources