Idle handler broken?

Hi,

The idle-handler seems broken, how to get older scripts working?

--Gimp autosave

on idle
	tell application "System Events"
		if exists process "X11" then
			set x11_app to true
		else
			set x11_app to false
		end if
		if exists process "Gimp" then
			set gimp_app to true
		else
			set gimp_app to false
		end if
		if x11_app is false or gimp_app is false then
			if x11_app is false then tell application "X11" to activate
			--delay 1
			if gimp_app is false then
				set sum_it to {}
				set target_f to ((path to pictures folder) & "Digital Kunst:" as text)
				tell application "Finder"
					repeat with i from 1 to number of items in alias target_f
						set this_item to item i of alias target_f
						if kind of this_item is "Gimp Image" then
							set this_d to modification date of this_item
							set name_of to name of this_item
							if sum_it is {} then
								set sum_it to this_d
								set the_name to name_of
							else
								if sum_it < this_d then
									set sum_it to this_d
									set the_name to name_of
								end if
							end if
						end if
					end repeat
					open alias (target_f & the_name)
				end tell
			end if
			return 3 * 60
		else if x11_app is true and gimp_app is true then
			beep
			delay 4
			--say "you've 4 seconds to skip to the main windows you want to save"
			tell process "Gimp" to keystroke "s" using control down
			return 12 * 60
		end if
	end tell
end idle

I see a bunch of problems. They’re mostly minor but I’ll tell you about them in the hopes it helps you. First, I see that you have everything inside of a “System Events” tell block. You’re telling the Finder and the X11 application to do stuff via System Events. I would suggest each application tell block to be separate from the others. Why tell System Events to tell the Finder to do something when you can tell the Finder directly?

Second, you’re adding strings wrong in this line.
set target_f to ((path to pictures folder) & “Digital Kunst:” as text)
"Digital Kunst: is a string and you want to add another string to it, but “path to pictures folder” is not a string so it must be converted to a string first so you can add them. So use this instead…
set target_f to (path to pictures folder as text) & “Digital Kunst:”

Third, in your Finder tell block you are querrying the folder many times in the repeat loop. I would query the folder once and then use that result in the loop. It should be more efficient. So I added this line and adjusted your repeat loop to use it.
set theItems to items of folder target_f

Last, when you perform a keystroke you have to make sure the application is frontmost because all keystrokes are performed on the frontmost application… even though you are telling a specific process it still only works on the frontmost application. So you must “activate” the application to make sure it is frontmost before performing a keystroke.
tell application “Gimp” to activate
tell application “System Events” to keystroke “s” using control down
By the way, is the keyboard shortcut to save from gimp control-s? Most applications use command-s.

With all this said, this is what I would suggest your idle handler to look like. I didn’t test it but hopefully it will fix your problem…

--Gimp autosave

on idle
	tell application "System Events"
		if exists process "X11" then
			set x11_app to true
		else
			set x11_app to false
		end if
		if exists process "Gimp" then
			set gimp_app to true
		else
			set gimp_app to false
		end if
	end tell
	if x11_app is false or gimp_app is false then
		if x11_app is false then tell application "X11" to activate
		--delay 1
		if gimp_app is false then
			set sum_it to {}
			set target_f to (path to pictures folder as text) & "Digital Kunst:"
			tell application "Finder"
				set theItems to items of folder target_f
				repeat with i from 1 to count of theItems
					set this_item to item i of theItems
					if kind of this_item is "Gimp Image" then
						set this_d to modification date of this_item
						set name_of to name of this_item
						if sum_it is {} then
							set sum_it to this_d
							set the_name to name_of
						else
							if sum_it < this_d then
								set sum_it to this_d
								set the_name to name_of
							end if
						end if
					end if
				end repeat
				open alias (target_f & the_name)
			end tell
		end if
		return 3 * 60
	else if x11_app is true and gimp_app is true then
		beep
		delay 4
		--say "you've 4 seconds to skip to the main windows you want to save"
		tell application "Gimp" to activate
		tell application "System Events" to keystroke "s" using control down
		return 12 * 60
	end if
end idle

Hi regulus,

thanks for your inspiring suggestions. Last time i search new methods to update and shorten my scripts, but with Gimp (free digital painting and retouching program for any platform) it’s a bit tricky : Gimp needs an window environment called X11, and Gimp itself is only a droplet to launch the Gimp program, originally developed for Linux. Gimp it isn’t a Carbon or Cocoa app, and i can’t call Gimp directly.
Gimp crashes sometimes, so i wrote this script do avoid data loss.
The problem was minor: i don’t know why, but the application has forgotten to resto open.