Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Apple > Operating Systems

  • Print
  • + Share This
From the author of Writing the Processing Code

Writing the Processing Code

Finally, we're ready to add the main code to the project. Click the AppDelegate.applescript file again to return to the main script.

The browseForPhoto handler will prompt the user to select an image. Once the image is selected, the handler will update the photoPath binding's value to the path and add a preview of the image to the image well on the interface. Update the browseForPhoto handler as follows:

on browseForPhoto_(sender)
      set thePath to (choose file with prompt "Please select an image:" without invisibles)
      set theImage to initWithContentsOfFile_(POSIX path of thePath) of alloc() of
       class "NSImage" of current application
      setPhotoPath_(thePath as string)
      setImage_(theImage) of imageView
end browseForPhoto_

The browseForOutputFolder handler will prompt the user to select an output folder. It will then set the outputFolderPath binding to the path. Update the browseForOutputFolder handler as follows:

on browseForOutputFolder_(sender)
      set thePath to (choose folder with prompt "Please select an output folder:" without invisibles)
      setOutputFolderPath_(thePath as string)
end browseForOutputFolder_

The processPhoto handler will retrieve all processing options in the interface. It will then open the image, using a background application in OS X named Image Events. With Image Events, the script will process the image according to the specified options (scaling, rotating, and so on) and then output the image in the appropriate format to the specified output folder. Update the processPhoto handler as follows:

on processPhoto_(sender)
      set thePhoto to photoPath as string
      set theOutputFolder to outputFolderPath as string
      set flipH to flipHorizontal as boolean
      set flipV to flipVertical as boolean
      set rotateImage to applyRotation as boolean
      set scaleImage to applyScaling as boolean
      set theRotationAmount to rotationAmount as integer
      set theScalingAmount to scaleAmount as integer
      set theUnits to scaleUnits as integer
      set theOutputType to outputType as integer

      if thePhoto = "" then return
      if theOutputFolder = "" then return
      if rotateImage = true then
            if rotationAmount = 0 then set rotateImage to false
      end if
      if applyScaling = true then
            if scaleAmount = 0 then set scaleImage to false
      end if

      tell application "Image Events"
            launch
            set theImage to open thePhoto
            tell theImage
                  set theImageName to name
                  flip horizontal flipH vertical flipV
                  if rotateImage = true then rotate to angle theRotationAmount
                  if scaleImage = true then
                        if theUnits = 0 then
                              scale to size theScalingAmount
                        else
                              scale by factor theScalingAmount / 100
                        end if
                  end if
                  set theSavePath to theOutputFolder & theImageName as string
                  if theOutputType = 0 then
                        save as BMP in (theSavePath & ".bmp")
                  else if theOutputType = 1 then
                        save as JPEG in (theSavePath & ".jpg")
                  else if theOutputType = 2 then
                        save as TIFF in (theSavePath & ".tif")
                  end if
                  close
            end tell
      end tell
end processPhoto_

Final Script

The completed script should appear as follows.

script AppDelegate
      property parent : class "NSObject"

      property photoPath : ""
      property outputFolderPath : ""

      property flipHorizontal : false
      property flipVertical : false
      property applyRotation : false
      property applyScaling : false

      property rotationAmount : 90
      property scaleAmount : 150

      property scaleUnits : 0
      property outputType : 1

      property imageView : missing value

      on applicationWillFinishLaunching_(aNotification)
            -- Insert code here to initialize your application before any files are opened
      end applicationWillFinishLaunching_

      on applicationShouldTerminate_(sender)
            -- Insert code here to do any housekeeping before your application quits
            return current application's NSTerminateNow
      end applicationShouldTerminate_


      on browseForPhoto_(sender)
            set thePath to (choose file with prompt "Please select an image:" without invisibles)
            set theImage to initWithContentsOfFile_(POSIX path of thePath) of alloc() of class
             "NSImage" of current application
            setPhotoPath_(thePath as string)
            setImage_(theImage) of imageView
      end browseForPhoto_

      on browseForOutputFolder_(sender)
            set thePath to (choose folder with prompt "Please select an output folder:" without invisibles)
            setOutputFolderPath_(thePath as string)
      end browseForOutputFolder_

      on processPhoto_(sender)
            set thePhoto to photoPath as string
            set theOutputFolder to outputFolderPath as string
            set flipH to flipHorizontal as boolean
            set flipV to flipVertical as boolean
            set rotateImage to applyRotation as boolean
            set scaleImage to applyScaling as boolean
            set theRotationAmount to rotationAmount as integer
            set theScalingAmount to scaleAmount as integer
            set theUnits to scaleUnits as integer
            set theOutputType to outputType as integer

            if thePhoto = "" then return
            if theOutputFolder = "" then return
            if rotateImage = true then
                  if rotationAmount = 0 then set rotateImage to false
            end if
            if applyScaling = true then
                  if scaleAmount = 0 then set scaleImage to false
            end if

            tell application "Image Events"
                  launch
                  set theImage to open thePhoto
                  tell theImage
                        set theImageName to name
                        flip horizontal flipH vertical flipV
                        if rotateImage = true then rotate to angle theRotationAmount
                        if scaleImage = true then
                              if theUnits = 0 then
                                    scale to size theScalingAmount
                              else
                                    scale by factor theScalingAmount / 100
                              end if
                        end if
                        set theSavePath to theOutputFolder & theImageName as string
                        if theOutputType = 0 then
                              save as BMP in (theSavePath & ".bmp")
                        else if theOutputType = 1 then
                              save as JPEG in (theSavePath & ".jpg")
                        else if theOutputType = 2 then
                              save as TIFF in (theSavePath & ".tif")
                        end if
                        close
                  end tell
            end tell
      end processPhoto_

end script
  • + Share This
  • 🔖 Save To Your Account