Smart rule not working

this smart rule isn’t working when I click on “Apply Rule” any reasons why?

tell application id "DNtp"
	try
		set this_selection to the selection
		if this_selection is {} then error "Please select some images."
		repeat with this_item in this_selection
			if the type of this_item is equal to picture then
				try
					set this_image to the image of this_item
					with timeout of 30 seconds
						tell application "Image Events"
							launch
							set this_file to open file this_image
							scale this_file by factor 0.5
							save this_file without icon
							close this_file
						end tell
					end timeout
				end try
			end if
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

That is not the correct form for smart rule scripts. You must be using the on performSmartRule() handler. This is added automatically in the smart rule action so you had to have deleted it. See the Automation chapter of the built-in Help and manual.

PS: Why are you tagging documents as resized before they’ve been resized?

1 Like

thank you, it worked now.

Another question, why is it when the action is “when opening” every time I open that item it resize it? Same thing with “after synchronization” any update to the file it resizes it.

it thats normal behaviour, then the action should be labeled “On open very time”

The script attached is to resize image

Each event is executed every time it happens and the conditions are fulfilled.

1 Like

Whats the best way to have this that should work correctly, I cant get it right. especially with “Perform the following action”

I have it After synchronization but its asking me to “select an image”

I want item to get resized then, move to a folder and finally remove tag.

The script is not intended for smart rules, see @bluefrog’s reply above.

what is the best way to resize each png item as it comes in without selecting it then?

resize to 50%

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			if (type of theRecord is picture) then -- Shouldn't be necessary if smart rule criteria is set up correctly. However, it's not bad to error trap your code.
				set recPath to (path of theRecord as string)
				
				tell application "Image Events"
					set theImage to open recPath
					set {w, h} to dimensions of theImage -- Using a variable here is to satisfy an issue with Image Events.
					scale theImage to size ((w / 2) as integer) -- Halving one dimension works as the scaling retains the aspect ratio of the original
					save theImage
				end tell
				
			end if
			set tags of theRecord to (tags of theRecord & "resized")
		end repeat
	end tell
end performSmartRule
2 Likes

Thank you

You’re welcome.