• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: getting height x width of image
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getting height x width of image


  • Subject: Re: getting height x width of image
  • From: "Mark J. Reed" <email@hidden>
  • Date: Wed, 20 Jun 2007 15:12:40 -0400

When I take a screenshot (cmd+shift+4) and it creates a file ~/Desktop/Picture 1.png
then the Folder Action will

        1) Rename it to have a unique filename based on the date/time it was taken
        2) move it to ~/Pictures/
        3) open the picture in Preview.app

I would like to add the height-x-width of the image to the filename so that it would look
 like this:

screenshot-344x208-2007-06-20_14.39.57.png

Kayo. I would use something other than periods in the time, though. Colons are bad from an HFS perspective, of course, but filenames with multiple periods can also confuse some things. Maybe more dashes.

        set theDate to do shell script "/bin/date \"+%Y-%d-%m_%H.%M.%S\"" --
get the date string
        -- is there a better way to get the date w/o using a shell script?

That depends on how you define "better". There are certainly ways to get a formatted date string within AppleScript without calling out to the shell, but they tend to be more verbose (unless you have an OSAX with a feature designed for the purpose).

        repeat with x in added_items
                --you always get a list, even if it's one file, so we loop through
the list here
                set theFile to x --x is just a list placeholder, let's use a better
name for what x refers to

Well, that's kind of silly. Why not just do

          repeat with theFile in added_items

in the first place?

                tell application "Finder" --since we're manipulating files, we use
the Finder

You can do a lot of file manipulation directly, without having to get the Finder involved. Depends on what you're doing.

                                try
                                        tell application "Image Events"
                                                -- start the Image Events application
                                                launch
                                                -- open the image file
                                                set theFile to open theFile

That doesn't work IMM. The open command in Image Events takes a pathname, not a file object.

It's also a bad idea to reuse "theFile"as both the file object and the
image object; in fact, that's probably the source of your
difficulties.  Settting the name of the image object doesn't rename
the file on disk, and Preview doesn't know what to do with the image
object either.

Try this:

tell application "Image Events"
   set theImage to open (theFile as string)
   copy dimensions of theImage to {W, H}
end

set the name of theFile to "screenshot-"& W & "x" & H & "-" & theDate
& "." & theFileExtension


-- Mark J. Reed <email@hidden> _______________________________________________ Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
  • Follow-Ups:
    • Re: getting height x width of image
      • From: TjL <email@hidden>
References: 
 >getting height x width of image (From: TjL <email@hidden>)

  • Prev by Date: Re: getting height x width of image
  • Next by Date: Re: getting height x width of image
  • Previous by thread: Re: getting height x width of image
  • Next by thread: Re: getting height x width of image
  • Index(es):
    • Date
    • Thread