Smart Rule script fails on external script, but not embedded

Tempted to chalk this one up to computer weather. I have a Smart Rule that “archives” files fitting a few conditions by moving them to a different database.

I wrote a script to do the moving. It works on selections (e.g., when debugging). It then failed (silently) when I attached it to the smart rule as an external script (e.g., I saved it in the Script folder’s Smart Rules subfolder and selected it in the drop-down menu, then hit “Apply rule” on the script when it showed some files fit the conditions).

In the course of debugging, I tried embedding the script—just copied and pasted the text from the external file to the embedded dialogue.

It worked.

I guess this is fine, but why? Any ideas?

Could you please post the source of the script? Thanks.

Sure thing:

on performSmartRule(theRecords)
tell application id "DNtp"
	repeat with eachRecord in theRecords
		set archiveDatabase to get database with uuid "ADB9D9D0-0304-43F4-A187-945F15A4777C"
		display dialog ("Confirm archival of \"" & name of eachRecord as string) & "\"."
		set theLocation to get location of eachRecord
		set sourceRecord to the parent of eachRecord
		set parentRecord to the first item of parent of eachRecord
		create location theLocation in archiveDatabase
		set destinationRecord to get record at theLocation in archiveDatabase
		
		-- create a link to the archive group, for convenience
		set destinationLink to reference URL of destinationRecord
		set destinationLinkRecordName to "Archive - " & name of parentRecord
		
		set searchRecords to (lookup records with path (path of parentRecord) & "/" & destinationLinkRecordName & ".inetloc") in sourceRecord
		log searchRecords
		if searchRecords is not {} then
			set archiveRecord to the first item in searchRecords
		else
			set archiveRecord to create record with {name:destinationLinkRecordName, type:bookmark, URL:destinationLink} in parentRecord
		end if
		
		if eachRecord is indexed then
			consolidate record eachRecord
		end if
		
		--move the record to the archive database
		move record eachRecord to destinationRecord from sourceRecord
		
	end repeat
end tell
end performSmartRule

Open to feedback, too! I’m sure it’s clunky.

I just tried this successfully using a simple smart rule that is performed on demand. Which macOS version do you use? By the way, user interaction in smart rule scripts is usually not recommended as smart rules should ideally work facelessly.

Hm. Computer weather it is, maybe… Using Catalina these days.

Good point re: the confirmation prompt. I wanted to make sure it worked as expected but I’ll disable it now.

Just a minor modification, but it’s working as an embedded script here…

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with eachRecord in theRecords
			set archiveDatabase to get database with uuid "ADB9D9D0-0304-43F4-A187-945F15A4777C"
			set theLocation to get location of eachRecord
			set parentRecord to parent 1 of eachRecord
			create location theLocation in archiveDatabase
			set destinationRecord to get record at theLocation in archiveDatabase
			
			-- create a link to the archive group, for convenience
			set destinationLink to reference URL of destinationRecord
			set destinationLinkRecordName to "Archive - " & name of parentRecord
			
			set searchRecords to (lookup records with path (path of parentRecord) & "/" & destinationLinkRecordName & ".inetloc") in parentRecord
			log searchRecords
			if searchRecords is not {} then
				set archiveRecord to the first item in searchRecords
			else
				set archiveRecord to create record with {name:destinationLinkRecordName, type:bookmark, URL:destinationLink} in parentRecord
			end if
			
			if eachRecord is indexed then
				consolidate record eachRecord
			end if
			
			--move the record to the archive database
			move record eachRecord to destinationRecord
			
		end repeat
	end tell
end performSmartRule
1 Like

Thanks! My original plan, though, was to get it working as an external script.

But this works! I am content.

Haha! I just noticed that. :roll_eyes: :stuck_out_tongue:

PS: I just set up my code as an external script and it worked as expected.

1 Like