Stay Open AppleScript with On Idle loop converting to AS Studio

I have a plain “vanilla” script that has an on idle loop to look at a folder every 5 seconds to see if a file has been placed in it to be processed. I thought I would move it into an AppleScript Studio app so I can have a window giving some feedback, etc.

I am having a hard time figuring out how to make an app that is constantly running and looking at a folder over and over. I tried a repeat loop instead of an on idle loop but it seems like there should be a better way of doing it. I definitely do NOT want to attach a folder action to the folder as I have found them to be horribly inconsistent. The app I made in Script Editor works really well. Can I do the same in XCode?

Here is the main format of my current script:

on run
	tell me to activate
	display dialog ("Tool Starting!") buttons " " default button 1 with icon 2 giving up after 2
end run

on idle
	set ConversionFolder to "MacintoshHD:ConversionFolder:" as alias
	try
		tell application "Finder" to set these_items to (files of (entire contents of ConversionFolder) whose kind contains "Quark")
	on error
		delay 1
		set these_items to {}
	end try
	if the number of items in these_items is greater than 0 then
		--Go through all files, one at a time
		repeat with this_item in these_items
			tell application "System Events" to set TheExtension to name extension of (this_item as alias)
			if TheExtension is not "" then
				tell (info for (this_item as alias)) to set FileName to name's text 1 thru ((my (offset of ("." & name extension) in name)) - 1)
			else
				tell (info for (this_item as alias)) to set FileName to name
			end if
			tell application "Finder" to set PathToOriginalFile to container of this_item as string
			set FinalPath to (PathToOriginalFile & FileName & ".indd")
			tell application "QuarkXPress"
				activate
				try
					open this_item use doc prefs yes remap fonts ask do auto picture import yes
				end try
				--Do the Quark stuff
				try
					close front document saving no
				end try
			end tell
		end repeat
	end if
	return 5
end idle

Model: G5 OSX 10.4.8
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Anyone??

This seems doable. Can you use an on idle loop in AS Studio? Or is there a better method?

Model: G5 OSX 10.4.8
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Duh. I always figure it out right after I post…

I hadn’t clicked on “Idle” under the “File Owner” to trigger the on idle loop. Now it appears to be working. I basically have an on launched to initialize the app and then the on idle loop with all of the main code.

Model: G5 OSX 10.4.8
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Alright. Next issue!

I now have an on launch which reads a folder location from the plist file (thanks to whoever that was that I got that from…) and the on idle loop works. It keeps looking at a folder to see if files have been placed in it.

Next I made a preferences window where you can change the folder location and also put it in “Test” mode. I am wondering if it is possible to basically make the idle loop stop doing what it is doing when the preferences window is opened. I can’t find any information about stopping and then restarting the on idle based on other events. Or, will opening another window basically mean that the app is not idle anymore so I don’t have to do anything?? Any insight would be appreciated. Thanks.

on launched theObject
	set VersionNumber to call method "objectForInfoDictionaryKey:" of (call method "mainBundle" of class "NSBundle") with parameters {"CFBundleShortVersionString"}
	tell window "mainWindow"
		set contents of text field "versionText" to VersionNumber
		set contents of text field "statusText" to "Initializing"
	end tell
	readPrefs()
end launched

on choose menu item theObject
	if name of theObject is "prefsMenu" then
		set visible of window "prefsWindow" to true
		tell window "prefsWindow" to set contents of text field "pathDisplay" to PathToFormData
	end if
end choose menu item

on readPrefs()
	if not (exists default entry "PathToFormData" of user defaults) then
		make new default entry at end of default entries of user defaults with properties {name:"PathToFormData", contents:"Macintosh HD"}
	else
		set PathToFormData to contents of default entry "PathToFormData" of user defaults
	end if
end readPrefs

on clicked theObject
	if name of theObject is "changePathPref" then
		tell window "prefsWindow"
			set PathToFormData to ((choose folder with prompt "Where is the Form Data folder:") as Unicode text)
			set contents of text field "pathDisplay" to PathToFormData
		end tell
		set contents of default entry "PathToFormData" of user defaults to PathToFormData
	end if
	if name of theObject is "Finished" then
		set visible of window "prefsWindow" to false
	end if
	if name of theObject is "testButton" then
		tell window "prefsWindow"
			if state of button "testButton" is 1 then
				set ThisIsATest to true
			else
				set ThisIsATest to false
			end if
		end tell
	end if
end clicked

on idle theObject
	set PathToDesktop to path to the desktop as text
	tell window "mainWindow"
		if contents of text field "statusText" is not "Idle" then set contents of text field "statusText" to "Idle"
	end tell
	set FormDataFolder to PathToFormData as alias
		try
			tell application "Finder"
				if (count of (every file of FormDataFolder whose kind contains "XML")) is greater than 0 then
					set these_items to (every file of FormDataFolder whose kind contains "XML")
				else
					set these_items to {}
				end if
			end tell
		on error
			delay 1
			set these_items to {}
		end try
	--Check to see if there are one or more XML files in the FormData folder on the desktop
	if the (count of these_items) is greater than 0 then
		tell window "mainWindow" to set contents of text field "statusText" to "Processing"
		
		--Go through all XML files, one at a time
		repeat with XMLfile in these_items
		--Do stuff
		end repeat
	end if
return 5
end idle

Model: G5 OSX 10.4.8
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Not sure if anyone is following this but, just in case, I came up with a solution. I added a repeat loop at the beginning of the On Idle loop to basically wait it the Preferences window is open. This could conceivably be a “Pause” button that could be checked to see if it is on or off, but for now I am only checking whether the prefs window is visible.

on launched theObject
   set VersionNumber to call method "objectForInfoDictionaryKey:" of (call method "mainBundle" of class "NSBundle") with parameters {"CFBundleShortVersionString"}
   tell window "mainWindow"
       set contents of text field "versionText" to VersionNumber
       set contents of text field "statusText" to "Initializing"
   end tell
   readPrefs()
end launched

on choose menu item theObject
   if name of theObject is "prefsMenu" then
       set visible of window "prefsWindow" to true
       tell window "prefsWindow" to set contents of text field "pathDisplay" to PathToFormData
   end if
end choose menu item

on readPrefs()
   if not (exists default entry "PathToFormData" of user defaults) then
       make new default entry at end of default entries of user defaults with properties {name:"PathToFormData", contents:"Macintosh HD"}
   else
       set PathToFormData to contents of default entry "PathToFormData" of user defaults
   end if
end readPrefs

on clicked theObject
   if name of theObject is "changePathPref" then
       tell window "prefsWindow"
           set PathToFormData to ((choose folder with prompt "Where is the Form Data folder:") as Unicode text)
           set contents of text field "pathDisplay" to PathToFormData
       end tell
       set contents of default entry "PathToFormData" of user defaults to PathToFormData
   end if
   if name of theObject is "Finished" then
       set visible of window "prefsWindow" to false
   end if
   if name of theObject is "testButton" then
       tell window "prefsWindow"
           if state of button "testButton" is 1 then
               set ThisIsATest to true
           else
               set ThisIsATest to false
           end if
       end tell
   end if
end clicked

on idle theObject
   set PathToDesktop to path to the desktop as text
   tell window "mainWindow"
       if contents of text field "statusText" is not "Idle" then set contents of text field "statusText" to "Idle"
   end tell

   repeat while visible of window "prefsWindow" is true
	tell window "mainWindow" to set contents of text field "statusText" to "Paused while updating preferences..."
	delay 1
   end repeat

   set FormDataFolder to PathToFormData as alias
       try
           tell application "Finder"
               if (count of (every file of FormDataFolder whose kind contains "XML")) is greater than 0 then
                   set these_items to (every file of FormDataFolder whose kind contains "XML")
               else
                   set these_items to {}
               end if
           end tell
       on error
           delay 1
           set these_items to {}
       end try
   --Check to see if there are one or more XML files in the FormData folder on the desktop
   if the (count of these_items) is greater than 0 then
       tell window "mainWindow" to set contents of text field "statusText" to "Processing"
       
       --Go through all XML files, one at a time
       repeat with XMLfile in these_items
       --Do stuff
       end repeat
   end if
return 5
end idle

Model: G5 OSX 10.4.8
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

I’m following it Matt!