Applescript to move incoming pictures to other folder with delay - macos

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.

Related

Renaming files after folder name

I need to rename multiple files after the folder that they are in using automator. e.g. index.html renamed to folder1.html
https://imgur.com/a/Cjgkn3V
I have about 900 folders with one file in them each all named index.html.
This is straight forward as using Terminal:
Open the Terminal app
type in "cd " (note the space) and then drag the folder containing all the folders on to the terminal window. Then press return.
Paste this in the terminal window and press enter:
for f in `ls -1`; do
if [[ -f $f/$index.html ]]; then
cp $f/index.html $f/$f.html;
fi;
done
Note that I am using the cp command here so it copies the index.html file instead of moving it. Just incase... :)
You can add this to a 'Run Shell Script' action in Automator. Just need to make sure your automator script is saved in the folder with all the sub folders.

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

Using bash to navigate to a file in Finder

I drag a lot of graphic files from Finder directly into InDesign and Photoshop. I use a vey simple bash script to quickly open the directory containing the file.
cd "/Volumes/Server/Resources/stock1/"
open .
The script opens the correct directory, but I would like to know how to get it to also go to a specified file (e.g., image.eps) and highlight/select it.
The directories I work with contain hundreds of files and have hard-to-look-through names. This would be a huge time-saver.
Thanks so much for any help. I'm using Mac OSX 10.9.5.
Use the -R (aka --reveal) option to select a single file:
open -R "/Volumes/Server/Resources/stock1/image.eps"
Something like,
open -R "/Volumes/Server/Resources/stock1/"*.eps
will not select all eps files in the folder, but instead will select each one successively, so that the end result is only the last file is selected.
#chepner's answer (-R option) is great if you want to highlight just one file. If you want to select multiple files, you may want to use Apple Script like this:
osascript -e 'tell application "Finder" to select files in folder "stock1" of folder "PHOTOS and IMAGES" of disk "Server" whose name ends with ".eps"'

How can I view the User/Library/LaunchDaemons folder within the finder?

Within the finder I can view the contents of User/Library and can see many folders there, however I cannot see a folder called LaunchDaemons.
However if I navigate to the Library folder from the terminal command line then LaunchDaemons is visible.
How can I make it appear in the Finder?
I've tried this command but it didn't have any effect
defaults write com.apple.Finder AppleShowAllFiles YES
There is (at least by default) no LaunchDaemons folder in the user Library. You may be navigating to one of the other Library folders in Terminal. You can check this with the commands pwd (which prints the full path to the current folder -- if it doesn't start with /Users/youraccountname/Library, you aren't in your user Library) and open . (which opens the current folder in the Finder -- and again, I'm pretty sure it won't be inside your user Library).

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

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

Resources