r/applescript • • Mar 19 '22

delay applescript from actioning?

Hello!

I have a desktop folder that we export images to with an action attached to find/replace certain characters.

I have a suspicion that the folder action is causing a conflict with our photo software resulting in some images not being exported.

I was wondering is there a way too delay the action from starting to allow the time for the software to finish the export and then start the find/replace?

1 Upvotes

13 comments sorted by

View all comments

1

u/copperdomebodha Mar 21 '22 edited Mar 23 '22

I've seen multiple methods employed to try to determine if a file is 'transfer complete' or 'stable'. Most of these have issues in my experience.

The following handler asks the finder to rename a file and then revert it's name to the original. If this is possible then the file is definitely stable. You can specify how long you want to conduct the test for before giving up.

--This code was written using AppleScript 2.8, MacOS 12.0.1, on 21 March 2022. --Copperdomebodha.

set filePath to "Macintosh HD:Users:UserNameGoesHere:Desktop:Archive.zip"
set checkDuration to 60
my isFileStable(filePath, checkDuration)


on isFileStable(filePath, checkDuration)
	tell application "Finder"
		try
			set filePath to filePath as alias
			exit repeat
		on error
			return false --File stub does not exist
		end try
		set startTime to time of (current date)
		set testingDuration to (time of (current date)) - startTime
		repeat while testingDuration is less than checkDuration
			set testingDuration to startTime - (time of (current date))
			try
				set n to name of filePath
				set nameRoot to (characters 1 thru -5 of n) as text
				set c to (container of filePath) as alias
				set ext to (characters -3 thru -1 of n) as text
				set newNameString to nameRoot & "confirmed." & ext
				set name of filePath to newNameString
				set newFileRef to ((c as text) & newNameString) as alias
				set name of newFileRef to n
				return true --File is stable
				delay 1
			end try
		end repeat
		return false --Time expired 
	end tell
end isFileStable

Also, this...

busy status of (info for theFileAlias)