Applescript folder actions support for changed / altered / updated files? - applescript

Does the folder action for when a folder item is changed not exist? I want my script to run when and if I update a file. I don't see any reference to it in the documentation. Is there some sort of alternative I am missing because this seams pretty crazy to not have.
on adding folder items to this_folder after receiving added_files
do shell script "anything"
end adding folder items to
on removing folder items from this_folder after losing removed_files
do shell script "anything"
end removing folder items from
-- does not exits?!?
on changing folder items in this_folder after updating changed_files
do shell script "anything"
end changing folder items in

Nope, doesn't exist directly. However, something similar could be accomplished with an idle handler that watches the files in the folder to see if their modification date has changed and perform an action on files where that's true.

There is an alternative to folder actions. You use launchd and setup a watch path. With a watch path, any time something changes in the folder you are watching, your code runs. The biggest difference between folder actions and the launchd action is that with the launchd action you don't know which files changed. You just know something changed. So your code has to figure out what the change actually was, but that shouldn't be too difficult in your case because if you're looking for an updated file you just check the modification date of the files.
You can google for launchd and watch paths if you want to try it.

What about rsync -va '/source/path/' '/destination/path/', using Lingon, have this simple command set as an user Daemon to run, say, every 10 sec?

I found a tricky way to do this with Automator easily and works only in some cases so try it and see if it helps. When you create/modify a folders contents in OSX a hidden OS file called .DS_Store gets written to the folder, its a useless file to a user but it can trigger the folder action. With that said, I use rsync in a Run Shell Script action in my folder action. Once the action is done syncing I then remove the .DS_Store file.
Here is my example:
rsync -r /Users/path/to/source/* /Users/path/to/destination
rm -f /Users/path/to/source/.DS_Store
Then the next time you modify files/folders in that directory, the folder action kicks in and the process would repeat.
I hope this helps...

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.

Folder action not triggering shell script

I have some software that exports a file called My Library.bib to a folder called thesis. Assume that the name of the exported file is fixed. Every time I export such a file, I want to:
Delete any old files called MyLibrary.bib if they exist.
Remove the space from this new file so that it becomes the up-to-date MyLibrary.bib.
I've tried making an Automator 'folder action' as follows:
... However, while the shell script works perfectly if run manually, the folder action itself never appears to trigger.
Folder actions are nonetheless enabled (see below settings), and other folder actions do seem to work.
Summarily, I just want any files named My Library.bib entering the thesis folder (at any time, automatically) to become renamed to MyLibrary.bib, replacing any existing MyLibrary.bib files. Any ideas what's going wrong, or how else to achieve this? Thanks in advance.
When you use the "Run Shell Script" action, the current directory is the Home folder, not the "thesis" folder.
So, you must use the cd command to change the current directory
Informations:
The "Get Folder Content" action is useless for what you want to do,
you can remove it.
The rm command is not necessary, you can use the mv -f to
overwrite an existing file
read firstLine ### get the path of the first dropped item
myDir=$(dirname "$firstLine") ### get the parent (this folder action)
cd "$myDir" && if [ -f "My Library.bib" ]; then
mv -f "My Library.bib" "MyLibrary.bib"
fi

How to create a batch file in Mac?

I need to find a solution at work to backup specific folders daily, hopefully to a RAR or ZIP file.
If it was on PC, I would have done it already. But I don't have any idea to how to approach it on a Mac.
What I basically want to achieve is an automated task, that can be run with an executable, that does:
compress a specific directory (/Volumes/Audio/Shoko) to a rar or zip file.
(in the zip file exclude all *.wav files in all sub Directories and a directory names "Videos").
move It to a network share (/Volumes/Post Shared/Backup From Sound).
(or compress directly to this folder).
automate the file name of the Zip file with dynamic date and time (so no duplicate file names).
Shutdown Mac when finished.
I want to say again, I don't usually use Mac, so things like what kind of file to open for the script, and stuff like that is not trivial for me, yet.
I have tried to put Mark's bash lines (from the first answer, below) in a txt file and executed it, but it had errors and didn't work.
I also tried to use Automator, but it's too plain, no advanced options.
How can I accomplish this?
I would love a working example :)
Thank You,
Dave
You can just make a bash script that does the backup and then you can either double-click it or run it on a schedule. I don't know your paths and/or tools of choice, but some thing along these lines:
#!/bin/bash
FILENAME=`date +"/Volumes/path/to/network/share/Backup/%Y-%m-%d.tgz"`
cd /directory/to/backup || exit 1
tar -cvz "$FILENAME" .
You can save that on your Desktop as backup and then go in Terminal and type:
chmod +x ~/Desktop/backup
to make it executable. Then you can just double click on it - obviously after changing the paths to reflect what you want to backup and where to.
Also, you may prefer to use some other tools - such as rsync but the method is the same.

rsync with AppleScript, copy from other volume

I'm trying to build a automatic backup solution. My AppleScript commands are pretty simple. All I want to do is copy all the files in one folder to a different folder.
Currently I'm using this:
rsync --update -raz --progress Documents/test "Volumes/RFM_Projects_2"
This works when I have my files located in a folder called test in the Documents folder. Problem is that I have a different folder structure now where I have all the files I want to copy on a second internal harddrive.
I thought it would be easy to just change "Documents/test" to something like "Volumes/WORK/test", but this does not work at all.
Any ideas about how I can change the source location to a secondary harddrive?
All slash-delimited style paths, like the ones used in your rsync command, must start with a slash (/). That slash stands for the startup volume drive. I do not see that first slash in any of your paths so I'm surprised any of your code ever worked. Here's an applescript which will show you the path. Run this, select any folder you want, and copy/paste the result into your code. Note that if there are any spaces in any of your paths then you should put your paths in quotes in your rsync command.
choose folder
return POSIX path of result
Also note if you want the path to a file instead of a folder then change the word folder to file in the applescript. Good luck.

Copy new files to another folder on windows automatically

I have a folder where some xml files keep updated, I want those files copied to another folder. From which a software collects the xml. I want to watch the folder and copy only new files as it comes to another folder automatically.
Is there any ready made solution or can it be done using Autoit, shell script or vb.net(last choice).
Main concerns:
Know when a file is added.
Copy them to the folder.
Make sure its notcopied second time.
If I schedule this code, it might also work
cd /home/webapps/project1/folder1 for f in *.csv do cp -v $f
/home/webapps/project1/folder2/${f%.csv}$(date +%m%d%y).csv done
But it again don't have a way to not copy those which are already copied.
rsync. Put it into cron to run every few minutes.

Resources