Retina Flight Check

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Retina Flight Check Mon Feb 13, 2012 8:50 am • by fruityth1ng
At work here, I often encounter the need to verify a folder of images that will end up in an iPhone App.
It's important for these files to be exactly double- or half the size of their counterpart.
This script, if saved as a separate script, can do just that using hazel.

Image

Further explanation: http://www.nimbling.com/blog/2012/02/automatic-flight-check-for-ios-retina-files/
*edit* you can download this script, as well as a single-file version here: http://www.nimbling.com/Downloads/Flight%20Check.zip
*edit2* I made a regular folder actions version of this, too! :)

Enjoy!

Code: Select all
global x_factor, y_factor, labelfile

on hazelProcessFile(theFile)
   tell application "Finder"
      launch
      tell application "System Events"
         set PosPAth to POSIX path of theFile
         set filename to name of (theFile as alias)
         set labelfile to theFile as alias
      end tell
      
      set ParentFolder to container of theFile -- sets the parent folder of the folder you select
      set ParentFoldertxt to ParentFolder as text
      set filename to name of file theFile -- sets the folder name as text
      set old_delimiters to AppleScript's text item delimiters
      set AppleScript's text item delimiters to {"."}
      set checkFile to first text item of filename as text
      set AppleScript's text item delimiters to old_delimiters
      tell me to set {my_x, my_y} to getsize(theFile)
      
      if checkFile contains "ipad" or checkFile contains "iPad" then
         set statusmsg to "iPad Asset"
         return statusmsg
      else if checkFile contains "@2x" then
         tell me to set filetwin to findAndReplace("@2x", "", checkFile) & ".png"
         set filetwin to ParentFoldertxt & filetwin
         if exists file filetwin then
            tell me to set {other_x, other_y} to getsize(filetwin)
            set x_factor to my_x / other_x
            set y_factor to my_y / other_y
            tell me to verifyTwinfactor(x_factor, y_factor, filetwin)
            set statusmsg to "standard_twin_found"
         else
            set statusmsg to "no_standard_file"
            set the label index of labelfile to 2 -- 2=RED
            return statusmsg
         end if
      else
         set filetwin to ParentFoldertxt & checkFile & "@2x.png"
         if exists file filetwin then
            tell me to set {other_x, other_y} to getsize(filetwin)
            set x_factor to other_x / my_x
            set y_factor to other_y / my_y
            tell me to verifyTwinfactor(x_factor, y_factor, filetwin)
            set statusmsg to "retina_twin_found"
         else
            set the label index of labelfile to 1 -- 1=ORANGE
            set statusmsg to "no_retina_file"
            return statusmsg
         end if
      end if
   end tell
end hazelProcessFile

on verifyTwinfactor(x_factor, y_factor, filetwin)
   tell application "Finder"
      launch
      if x_factor as real is 2.0 and y_factor as real is 2.0 then
         tell application "Finder"
            set the label index of labelfile to 6 -- 0=NONE 6=GREEN
            set the label index of file filetwin to 6 -- 0=NONE 6=GREEN            
         end tell
         set statusmsg to "twin_ok"
         return statusmsg
      else
         tell application "Finder"
            set the label index of labelfile to 5 -- PURPLE
         end tell
         set statusmsg to "twin_bad"
         return statusmsg
      end if
   end tell
end verifyTwinfactor

on getsize(theFile)
   tell application "Image Events"
      launch
      set my_image to open theFile
      set my_dimensions to dimensions of my_image
      close my_image
      set my_x to item 1 of my_dimensions as integer
      set my_y to item 2 of my_dimensions as integer
      quit
      return {my_x, my_y}
   end tell
end getsize

on findAndReplace(tofind, toreplace, TheString)
   set ditd to text item delimiters
   set text item delimiters to tofind
   set textItems to text items of TheString
   set text item delimiters to toreplace
   if (class of TheString is string) then
      set res to textItems as string
   else
      set res to textItems as Unicode text
   end if
   set text item delimiters to ditd
   return res
end findAndReplace
fruityth1ng
 
Posts: 21
Joined: Mon Oct 12, 2009 7:26 am

Re: Retina Flight Check Thu Feb 16, 2012 2:01 pm • by Mr_Noodle
Not sure how many iOS developers we have around here but I'm sure they'll find something like this helpful. Thanks.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Tips & Tricks - DO NOT POST QUESTIONS