• 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: Image Events Caching issue
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Image Events Caching issue


  • Subject: Re: Image Events Caching issue
  • From: kai <email@hidden>
  • Date: Sat, 20 Jan 2007 19:27:08 +0000


On 20 Jan 2007, at 01:55, Jesse Almanrode - JA Computing wrote:

I am writing a script to gather dimension information from multiple
images. However, when I assign a variable to the dimensions of the
first image the stick forever. Say the first image is 1920x1200 and
the second image is 1024x768, the first image registers properly as
1920x1200 but the second image does not. Rather, Image Events returns
that it has the same dimensions as the first image which is not true.
I have to actually tell Image Events to quit to clear its "cache" but
then my script errors out because a variable's value is not defined.

Any ideas?

One or two, Jesse. :-)

The first part of the problem relates specifically to what we're trying to open and close in Image Events.

From your script:

tell application "Image Events"
	open theImage

The value of 'theImage' is actually an alias - which is just what Image Events expects to open. So far so good. (All the same, it might be worth considering an alternative label, such as 'theAlias', to help avoid any potential confusion).


set imageSize to dimensions of front image

Here, we're getting the dimensions of the front image - which should work fine, as long as there are no other images present. But somewhat untypically, Image Events opens a new image *behind* any existing ones. So to specify the most recent image by index, we really need to use something like "last image" or "image -1". Otherwise, the referenced image will be the first of the current batch to have been opened (hence the "sticking" value that you've been experiencing).


An even more reliable method is to use the value returned from the 'open' command - which is a reference to the image from the opened file. (See the example further below.)

close theImage

In this situation (bearing in mind that the variable 'theImage' represents an alias), the command fails (silently). This is simply because Image Events expects to close an image, rather than the alias presented. And because the intended image doesn't get closed, subsequent iterations of the repeat loop will continue to return the same dimensions, from exactly the same front image.


We therefore need to make sure that the correct image is identified and closed - either by referring to the last image, or by using the 'close every image' (or 'close images') command. Again, using the image reference provided by the 'open' command, should also be reliable.

end tell

The second issue, that of an error following a quit and script rerun, is down to Image Events' apparent reluctance to launch implicitly. When writing or editing a script that contains Image Events tells/ terminology, this behaviour may be masked - since, during script compiling/decompiling, a launch is usually invoked to gain access to the application's dictionary. However, that's by no means the case when running a script application, or following a quit.


The safest way around this is to issue an explicit launch command before any other call to Image Events. The application normally quits automatically 5 minutes after launch, too - so it pays to precede any series of commands in this way, even if that entails repeated launch commands. (When a launch event is sent to an application that is already running, it's ignored anyway.)

A couple of further, incidental points: Since the parameter value of a script application's open handler is already a list of aliases, it shouldn't be necessary to coerce each item to an alias. Also, if we opt to perform folder creation and file movement operations using a shell script, there's really no need for a Finder tell block.

Here's an example that considers some of these issues:

-----------------

on open aliasList

	repeat with theAlias in aliasList

		-- perform any preliminary operations

		tell application "Image Events"
			launch
			set theImage to open theAlias
			set imageWidth to item 1 of (get theImage's dimensions)
			close theImage
		end tell

		-- perform any subsequent operations
		-- do something with imageWidth

	end repeat

end open

-----------------

That said, and observing that you appear to have a predilection for shell scripts, you might like to consider using the scriptable image processing system (sips) in place of Image Events (which actually uses the functionality of sips, anyway).

Here's an example - along with a couple of variations, just for good measure. Firstly, it includes the -n option for 'mv'. This simply means that, when attempting to move a file that would otherwise cause the overwriting of an existing one, the move is aborted. In addition, this version creates a new folder (assuming one doesn't already exist) - but only when an appropriate image requires it. These suggestions may or may not be relevant to your aims - but I'll leave you to play with them, anyway...

-----------------

property folderList : {"800", "1024", "1280", "1600", "1920"}
property sizeList : {1000, 1200, 1400, 1800, 7243} (* no ceiling on last value *)
property sizeCount : count sizeList


on open aliasList
	repeat with theAlias in aliasList
		set imagePath to quoted form of POSIX path of theAlias
		set imageWidth to (word -1 of (do shell script ¬
			"/usr/bin/sips -g pixelWidth " & imagePath)) div 1
		repeat with i from 1 to sizeCount
			if imageWidth < sizeList's item i then exit repeat
		end repeat
		set targetPath to " ~/Desktop/" & folderList's item i
		do shell script "/bin/mkdir -p" & targetPath & ¬
			"; /bin/mv -n " & imagePath & targetPath
	end repeat
end open

-----------------

Here is the script... simple but soon to be more powerful


property SIZE_LIST : {"800", "1024", "1280", "1600", "1920"}

on open theList
	tell application "Finder"
		repeat with x in SIZE_LIST
			try
				do shell script "mkdir ~/Desktop/" & x --Create new directories
for images to get organized into
			end try
		end repeat
		repeat with x in theList
			set theImage to x as alias
			tell application "Image Events"
				open theImage
				set imageSize to dimensions of front image
				close theImage
			end tell

			set imageWidth to item 1 of imageSize as number

			if imageWidth < 1000 then
				set imagePath to quoted form of POSIX path of theImage
				do shell script "mv " & imagePath & " ~/Desktop/800"
			else if imageWidth ≥ 1000 and imageWidth ≤ 1200 then
				set imagePath to quoted form of POSIX path of theImage
				do shell script "mv " & imagePath & " ~/Desktop/1024"
			else if imageWidth ≥ 1200 and imageWidth ≤ 1400 then
				set imagePath to quoted form of POSIX path of theImage
				do shell script "mv " & imagePath & " ~/Desktop/1280"
			else if imageWidth ≥ 1400 and imageWidth ≤ 1800 then
				set imagePath to quoted form of POSIX path of theImage
				do shell script "mv " & imagePath & " ~/Desktop/1600"
			else if imageWidth ≥ 1800 then
				set imagePath to quoted form of POSIX path of theImage
				do shell script "mv " & imagePath & " ~/Desktop/1920"
			end if
		end repeat
	end tell
end open

--- kai


_______________________________________________ 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/mailman//archives/applescript-users

This email sent to email@hidden
References: 
 >Image Events Caching issue (From: "Jesse Almanrode - JA Computing" <email@hidden>)

  • Prev by Date: How to select a message in Mail
  • Next by Date: Photoshop warp and smart object script - is that possible?
  • Previous by thread: Re: Image Events Caching issue
  • Next by thread: AppleScript. launch executable without waiting for it to terminate
  • Index(es):
    • Date
    • Thread