Delete most recently added directory to a directory in applescript - 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

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.

AppleScript duplicate folder inside pre-existing folder

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"

Batch Command to Copy Group Policy User Files to Sub-Folder with Unknown Name

I'm working on a project where I need to sysprep a windows 7 image. I need to create a batch script to delete the existing files and sub folders inside another folder with an unknowable name located within the windows/system32/GroupPolicyUsers folder and then copy the needed user policies back into the windows/system32/GroupPolicyUsers/unkownFolderName folder and finally run gpupdate /force.
There is only a single sub folder in the /windows/system32/GroupPolicyUsers folder, but the name of that folder changes to the SID of the user after sysprep which is why I cannot know the exact name of the folder.
Inside this folder having the unknown name there is one sub folder and one file that needs to be deleted and replaced with my own folder and file.
Is this possible using a batch script?
Thanks in advance for your replies...

Windows script to delete folder

I need to have windows script to delete folder & files those are older by x days than the last modified folder, So if there are only the folders modified by same date then no folder should be deleted, else if there is any folder older by x days than last modified folder date needs to be deleted.
Could someone please response on it?

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

Resources