Hello. I’ve written a short Applescript that renames screenshots and moves the renamed file to the Desktop (from /users/username/pictures/screenshots/). The Applescript works fine if run manually. I’ve attached it to a Folder Action linked to the /users/davidallie/pictures/screenshots folder, but whenever a new screenshot is created, the Applescript doesn’t run.
I’m hoping that someone might know what I’m missing in the Applescript. Here it is:
-- rename_screenshots_01
--
-- THIS applescript changes the filename to my preferred format
-- and then moves it from a user-specified screenshots folder
-- to the desktop folder.
--
-- david.allie
-- 20130306 at 1857
--
tell application "Finder"
set screenshotsFolder to "Mac_HD:Users:davidallie:Pictures:screenshots"
set desktopFolder to "Mac_HD:Users:davidallie:Desktop"
set name of file (do shell script ("cd /users/davidallie/pictures/screenshots;ls *.png")) of folder screenshotsFolder to "screenshot_" & (do shell script "date '+%Y%m%d'") & "_" & (do shell script "date '+%H%M%S'") & ".png"
move every file of entire contents of folder screenshotsFolder to folder desktopFolder
end tell
Model: Mac Pro dual-quad-core Xeon, 12GB RAM, 8GB HD
AppleScript: 2.5
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)
Have you set up the folder as having a Folder Action? You do that with Folder Actions Setup which is found in ~/Library/Scripts/Folder Action Scripts/ or you can get it by right-clicking on the target folder and choosing Folder Action Setup . from the Services menu. In either case a window opens with a drop-down sheet listing AppleScripts that you can attach to the folder. Select your script and click on Enable Folder Actions and Quit Setup.
Does it work then?
I much prefer droplets to Folder Actions which can be flakey if the Finder is busy elsewhere. Things can be missed. Better yet (for money) is a program called Hazel.
This script doesn’t match the basic requirements about the structure of a folder actions one.
This one is :
on adding folder items to this_folder after receiving added_items
– the code
end adding folder items to
I wish to add that renaming the newly adde file in the host folder is bad practice because the renamed file will appear as an other added file and restart the process.
Renaming a file trucmuche.jpg as trucmuche.png will not change it into a png one.
It’s easy to define the format and the location of the screenshots created with the standard shortcuts.
I will not explain that here because I’m only half awake : insomnia is at work
Yvan KOENIG (VALLAURIS, France) jeudi 7 mars 2013 03:32:01
THIS solved it. I added the above two lines and my Folder Action works fine now. For the record, the completed Applescript is now:
-- rename_screenshots_01
--
-- THIS applescript changes the filename to my preferred format
-- and then moves it from a user-specified screenshots folder
-- to the desktop folder.
--
-- david.allie
-- 20130306 at 1857
--
on adding folder items to this_folder after receiving added_items
set screenshotsFolder to "Mac_HD:Users:davidallie:Pictures:screenshots"
set desktopFolder to "Mac_HD:Users:davidallie:Desktop"
tell application "Finder"
set name of file (do shell script ("cd /users/davidallie/pictures/screenshots;ls *.png")) of folder screenshotsFolder to "screenshot_" & (do shell script "date '+%Y%m%d'") & "_" & (do shell script "date '+%H%M%S'") & ".png"
move every file of entire contents of folder screenshotsFolder to folder desktopFolder
end tell
end adding folder items to
The code sample you provided resolved the problem.
The applescript isn’t saving it to the same folder, but to a different folder.
The default extension for screenshots on my Mac is .png, not .jpg, and I’m aware that if I was just changing the extension that it wouldn’t change the format. Since my screenshots are already in .png, there’s no conversion happening here, just a change in filename and move to a different folder.
Thanks for taking the time to help out even when you were half-awake!
Your script is renaming the file in the original folder. This is what is bad practice (at least from my point of view).
Here is an old one which is far from perfect but it describe :
(1) the good workflow ( don’t rename in the source folder)
(2) don’t rely upon the added_items list passed by folder actions. It treat the list of documents existing in the source folder which may contain several ones due to the fact that the actions are a bit lazy.
Your script try to do the same but what is it doing if there are two png files in the folder ?