Previous 1 2 3 Next 31 Replies Latest reply: Dec 4, 2012 4:58 PM by adayzdone Go to original post
  • twtwtw Level 5 Level 5 (4,900 points)

    Oooo, good point.  It would probably be ok since it's reusing the same openedFile variable (that should automatically release the last image data), but to be on the safe side ludwip could replace the save line with the following:

     

      close saving yes


    that would definitely avoid any such problems.  He might even want to add the following lines to the on error section:

     

      try

                close openedFile saving no

      end try


    which will try to close any opened image in the event of an error.

  • ludwip Level 1 Level 1 (0 points)

    twtwtw wrote:

     

    Oooo, good point.  It would probably be ok since it's reusing the same openedFile variable (that should automatically release the last image data), but to be on the safe side ludwip could replace the save line with the following:

     

      close saving yes


    that would definitely avoid any such problems.  He might even want to add the following lines to the on error section:

     

      try

                close openedFile saving no

      end try


    which will try to close any opened image in the event of an error.

     

    Thanks twtwtw,

     

    Just to stay on the safe side.

    Does the code supposed to look like this?

     

    Thanks a lot for your input.

     

    set folderToProcess to choose folder with prompt "Choose a folder to process"

    set folderPath to quoted form of POSIX path of folderToProcess

     

    -- 13 is a spotlight constant for images that I can't find documented anywhere (I got it by saving a spotlight image search in the Finder and examining it in a text editor).  FYI.

    set fileList to paragraphs of (do shell script "mdfind -onlyin " & folderPath & " '_kMDItemGroupId == 13'")

     

    tell application "Image Events"

      -- tell image event not to quit itself

              set quit delay to 0

     

              repeat with thisImageFile in fileList

                        try

      -- open the image file

                                  set openedFile to open thisImageFile

     

                                  tell openedFile

      -- you didn't specify which way or how much you wanted to rotate.  this rotates them 90° clockwise

      rotate to angle 90

      -- save back to same file

      close saving yes

                                  end tell

                        on error errstr

      -- image events had an error - dislpay alert, then skip this file

                                  my displayError(thisImageFile, errstr)

                                  try

      close openedFile saving no

                                  end try

                        end try

              end repeat

     

      -- reset Image Event's automatic quitting

              set quit delay to 900

    end tell

     

    on displayError(thisImageFile, errstr)

              tell application "System Events"

                        display alert "Image Events Error" message "file : " & thisImageFile & return & return & "error: " & errstr

              end tell

              set imageAlias to POSIX file thisImageFile

              tell application "Finder"

                        set label index of item imageAlias to 1

              end tell

    end displayError

  • ludwip Level 1 Level 1 (0 points)

    It doesn't work than.

    It starts OK but after a while it stops (application stops responding) and I must Force Quit it.

    Thanks

  • twtwtw Level 5 Level 5 (4,900 points)

    well, I just ran it on a copy of my entire pictures folder from the script editor and it worked fine.  You didn't mention you wanted to run this from an app, and I suspect any app you run it from is going to look like it's frozen - applescript isn't threaded, so it will just chug away unresponsively until it's done, which may take a while.

     

    if you want to be sure it's working and not frozen, you can add some audio feedback.  set i to 0 before the repeat loop, then add in a vocal counter before the end repeat statement.

     

              set i to 0

              repeat with thisImageFile in fileList

      --  yaddayaddayadda

     

                        set i to i + 1

                        if (i mod 300) = 0 then

                                  say (((i * 1000) div (count of fileList)) / 10 & " percent done") as text

                        end if

              end repeat


  • ludwip Level 1 Level 1 (0 points)

    Thanks twtwtw,

     

    Just to let you know, I don't run script in any other application than AppleScript Editor.

     

    It is working almost perfect now.

     

    The only problem that I've experienced is related to an error message. If I don't dismiss the message on time it will finish script before images are rotated. Is there any chance for the script just to ignore this kind of errors?

     

    Thanks

  • twtwtw Level 5 Level 5 (4,900 points)

    just comment out the display dialog line (seventh line from the end, add two dashes (--) at the front of the line). The scirpt will still mark missed files in the finder, but won't put up a dialog that has to be dismissed.

  • adayzdone Level 2 Level 2 (150 points)

    ludwip,

     

    Did this not work?

     

    set mainFolder to "/Users/ludwip/Documents/My Folder"

    do shell script "find " & quoted form of mainFolder & " \\! -name \".*\" -type f -print0 | xargs -0 sips -r 90"

  • ludwip Level 1 Level 1 (0 points)

    Hi adayzone,

    I was testing it but AppleScript stops occasionally.

    I'm wandering if it recognises all types of image files or just jpg?

    Thanks

  • ludwip Level 1 Level 1 (0 points)

    Hi VikingOSX,

     

    I was trying to use the script, but I must be doing something wrong in the terminal I guess.

     

    Should there be any dialog box or anything else where I set folder for the script to work?

     

    Thanks

    VikingOSX wrote:

     

    Thanks for your imput.

    However there are two scipts in your post.

    This one: '

    chmod +x rotate.sh

    ./rotate.sh'

    as well as former (longer one). Which one should I use?

     

     

    There are two distinct commands to run in the terminal. You do both, on separate command lines as shown. The assumption is that you are currently in your home login directory. Again, I would advise running this on a few files, then stop, and analyze the files to ensure you are satisfied. Then apply it to a larger subset, and review. Then to all images you wish to rotate.

     

    1. chmod +x rotate.sh
      1. Use the chmod OS X command to change the script permissions to executable
      2. This does not run the script or alter contents.

    2. ./rotate.sh
      1. Start the rotate.sh script
      2. The script will prompt you for the directory path to the main image folder where you wish to begin
        1. If on an externally mounted drive, then /Volumes/drivename/path-to-image-folder
        2. This script does not use restricted system syntax to operate on your image files, as it expects that their access privileges are already owned by your login name.
  • twtwtw Level 5 Level 5 (4,900 points)

    sips handles only the following formats (from its man page):

     

    jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga

     

    if you feed it the wrong kind of file it will throw an error, any I've never understood xargs well enough to know what it will do if the utility it's running throws an error.

     

    THe find command he's using will find every file that happens to have an extension; are their any other types of files in that directory?

  • ludwip Level 1 Level 1 (0 points)

    Adyzone,

    This actually worked really fast and without errors.

     

    Many thanks!

     

     

    adayzdone wrote:

     

    ludwip,

     

    Did this not work?

     

    set mainFolder to "/Users/ludwip/Documents/My Folder"

    do shell script "find " & quoted form of mainFolder & " \\! -name \".*\" -type f -print0 | xargs -0 sips -r 90"

  • adayzdone Level 2 Level 2 (150 points)

    Happy to help twtwtw correctly points out that the command will try to process all files in the main directory. (which do not start with . )    You can change the find command to limit the scope to files that end with jpg, etc.

  • ludwip Level 1 Level 1 (0 points)

    Thanks adayzdone,

    Can you please let me know what should I do in order to change the scope?

    Thanks

  • adayzdone Level 2 Level 2 (150 points)

    Like this:

     

    do shell script "find " & quoted form of mainFolder & " -iname *.jp*g -o -iname *.pdf -o -iname *.tiff"
    

     

    .jp*g matches jpg or jpeg