Adrian Nier Code

Back to overview

Export CandyBar selection

Last modification: Sunday, November 01, 2009 10:06 am

This script will export the current CandyBar selection in multiple sizes.

Implementation

on run
   if requireCandyBar() is false then return false
   set _sizes to askForSizes()
   exportToTIFFWithSizes(_sizes)
end run

on requireCandyBar()
   tell application "System Events" to set _processExists to (exists process "CandyBar")
   if _processExists then
      return true
   else
      activate
      display alert "CandyBar not running" message "This script exports the currently selected CandyBar item to multiple TIFF sizes. Please launch CandyBar before running this script." buttons {"OK"} default button 1 as warning
      return false
   end if
end requireCandyBar

on askForSizes()
   set _sizes to {16, 32, 128, 256, 512}
   set _chosenSizes to (choose from list _sizes with prompt "Select the sizes to export to:" default items _sizes OK button name "Export" with multiple selections allowed)
   return _chosenSizes
end askForSizes

on exportToTIFFWithSizes(_sizes)
   if _sizes is false then return false
   repeat with _i from 1 to count of _sizes
      exportToTIFFWithSize(item _i of _sizes)
   end repeat
end exportToTIFFWithSizes

on exportToTIFFWithSize(_size)
   enterTIFFExportDialog()
   waitForSheet()
   goToDesktopFolder()
   setExportSize(_size)
   addExportSizeToFileName(_size)
   dismissExportDialog()
end exportToTIFFWithSize

on enterTIFFExportDialog()
   tell application "System Events"
      tell process "CandyBar"
         set frontmost to true
         tell menu bar 1
            tell menu bar item 3
               click
               tell menu 1
                  click
                  tell menu item "Export"
                     click
                     tell menu 1
                        click
                        tell menu item "TIFF…"
                           click
                        end tell
                     end tell
                  end tell
               end tell
            end tell
         end tell
      end tell
   end tell
end enterTIFFExportDialog

on waitForSheet()
   repeat 100 times
      tell application "System Events"
         tell process "CandyBar"
            tell window 1
               if (exists sheet 1) then exit repeat
            end tell
         end tell
      end tell
      delay 0.1
   end repeat
end waitForSheet

on goToDesktopFolder()
   tell application "System Events"
      tell process "CandyBar"
         set frontmost to true
         keystroke "d" using command down
      end tell
   end tell
end goToDesktopFolder

on setExportSize(_size)
   tell application "System Events"
      tell process "CandyBar"
         tell window 1
            tell sheet 1
               tell group 1
                  if _size is 16 then
                     tell slider 1 to set value to 1
                  else if _size is 32 then
                     tell slider 1 to set value to 2.046979904175
                  else if _size is 48 then
                     tell slider 1 to set value to 3.013422727585
                  else if _size is 128 then
                     tell slider 1 to set value to 5
                  else if _size is 256 then
                     tell slider 1 to set value to 7
                  else if _size is 512 then
                     tell slider 1 to set value to 9
                  end if
               end tell
            end tell
         end tell
      end tell
   end tell
end setExportSize

on addExportSizeToFileName(_size)
   tell application "System Events"
      tell process "CandyBar"
         tell window 1
            tell sheet 1
               tell text field 1
                  set value to value & " " & (_size as string)
               end tell
            end tell
         end tell
      end tell
   end tell
end addExportSizeToFileName

on dismissExportDialog()
   tell application "System Events"
      tell process "CandyBar"
         tell window 1
            tell sheet 1
               tell button 1
                  click
               end tell
            end tell
         end tell
      end tell
   end tell
end dismissExportDialog