TYPO free


fighting for TYPO free code

Create a 'drop shadow' folder action

13 Jan 2009

Back in the day I worked for an advertising company and had a desk in the studio where all the shiny Macs lived. They had all these cool Adobe programs that could do amazing things for you when hooked to 'folder actions'. I never cared a lot about having those cool tools for myself, but now I write manuals occasionally and I wanted to have a nice folder action to add a drop shadow to screenshots.

Tuning OSX screenshots

OSX adds shadows to screenshots automagically. I don't like those since they are big as in 20px wide. So first I disabled those using the following command (typed into a terminal window):

defaults write com.apple.screencapture disable-shadow -bool true
killall SystemUIServer

You can also adjust the screenshot image type and location using the 'defaults' command. Instead of 'jpg' you can use png or gif for example.

defaults write com.apple.screencapture type jpg
defaults write com.apple.screencapture location "/Users/username/Desktop/Shots"
killall SystemUIServer 

Adding drop shadows using a headless Gimp

Then I used 'the Gimp' because it is cheap (as in Free Beer). The latest versions (2.6) finally handles color profiles in a decent way. You can install the latest Gimp version using MacPorts.

I used the 'drop shadow' (under Filters -> Light and Shadow). I was happy for a while . . . until I was writing the DAM manual for TYPO3. Manually adding all those drop shadows to the screenshots started to hurt. Thinking back of the days when I worked for the other employer I started to search for a way to get Gimp to add a drop shadow when dropping a screenshot file onto a folder.

The gimp can run in 'batch mode' and use 'scripts' to do anything you would normally do through a GUI. You can automate any task for which no visual pinpointing or manual path selection is needed. Here is a script in 'scheme' (the language Gimp uses for scripts) to create a drop shadow for an image and then save it as a 88% quality jpg file:

  1. ; Drop shadow script: batch-drop-shadow.scm
  2. ; Michiel Roos
  4. (define (batch-drop-shadow pattern
  5. offsetx
  6. offsety
  7. radius)
  8. (let* ((filelist (cadr (file-glob pattern 1))))
  9. (while (not (null? filelist))
  10. (let* ((filename (car filelist))
  11. (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
  12. (drawable (car (gimp-image-get-active-layer image))))
  13. (script-fu-drop-shadow image drawable offsetx offsety radius '(0 0 0) 80.0 TRUE)
  14. (set! drawable (car (gimp-image-flatten image)))
  15. (file-jpeg-save RUN-NONINTERACTIVE image
  16. drawable
  17. filename
  18. filename
  19. 0.88 ;quality
  20. 0
  21. TRUE
  22. FALSE
  23. ""
  24. 1
  25. FALSE
  26. FALSE
  27. 2
  28. )
  29. (gimp-image-delete image)
  30. )
  31. (set! filelist (cdr filelist))
  32. )
  33. )
  34. )

Place this script in your scripts directory. On my system this is in ~/.gimp-2.6/scripts. If you don't have that folder, create it first.

If you did this correctly, you can now call the script, feeding it an image file. The parameters are offsetx, offsety and radius (set to 8, 8 and 10 below). 

/opt/local/bin/gimp --no-data -i -b '(batch-drop-shadow "Picture 1.jpg" 8 8 10)' -b '(gimp-quit 0)' 

This should result in a shiny drop shadow added to the screenshot.

Creating a folder action

Now we need to tie this to a 'folder action'. If you do not have folder actions enabled, enable them using "/Applications/AppleScript/Folder Actions Setup". Place this script in the folder: "~/Library/Scripts/Folder Action Scripts".

  1. (*
  2.  Image - drop shadow
  4.  This Folder Action handler is triggered whenever items are added to the attached folder.
  5.  The script creates a drop shadow around the image.
  7.  Copyright © 2008 Michiel Roos
  8.  *)
  9. property type_list : {"JPEG"}
  10. property extension_list : {"jpg", "jpeg"}
  12. on adding folder items to this_folder after receiving these_items
  14. try
  16. process_items(this_folder)
  17. on error error_message number error_number
  18. if the error_number is not -128 then
  19. tell application "Finder"
  20. activate
  21. display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
  22. end tell
  23. end if
  24. end try
  25. end adding folder items to
  27. -- this sub-routine processes files
  28. on process_items(this_folder)
  29. -- NOTE that the variable this_item is a file reference in alias format
  30. try
  31. -- convert alias reference to string
  32. --set this_item to this_item as string
  33. with timeout of 900 seconds
  34. do shell script "/opt/local/bin/gimp --no-data -i -b '(batch-drop-shadow \"" & POSIX path of this_folder & "*.jpg\" 8 8 10)' -b '(gimp-quit 0)'"
  35. end timeout
  36. on error error_message
  37. tell application "Finder"
  38. activate
  39. display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
  40. end tell
  41. end try
  42. end process_items

Now you are ready to assign this script to a folder of your choice. I have a folder inside my 'Shots' folder that is called 'add drop shadow'. You can right-click the folder and choose 'More' from the context menu. There you can 'Enable folder actions' and 'Configure folder actions'.

So now you can make a bunch of screenshots and drop them of the 'drop shadow' folder. A drop shadow will automagically be added to your screenshots.

Don't forget to move the images out of the folder again. I'm sure there is room for improvement here, but this solution has saved me a lot of time.

add comment
Commenting is closed for this item