Pause/Block script until file exists

Hi,

I’m having trouble getting an AS program to “wait” for a file to exist before moving on.

Here’s the snippet of code that is causing the problem:


tell application "Safari"
	open location theURL
end tell

try
	repeat
		if not (alias fullSrcFile exists) then
			return
		else
			delay 5
		end if
	end repeat
on error theError
	display dialog theError
end try

To briefly explain fullSrc||File is the complete path of where Safari will put the file once downloaded.

Now I believe I have two problems. The first is that Safari does download the file (it’s a music track) but it does appear to generate an error in doing so.

Secondly the check to see if the file exists always causes an error so the dialog box is displayed. The error message is file not found.

What I want to do is make the script pause until Safari has finnished downloading the file. Any tips on how best I could this? I also want to automatically close the tap Safari opens when downloading the track. :wink:

Cheers

internet_pixie

What about this?


tell application "Finder"
   repeat
      if (file fullSrcFile exists) then
         exit repeat
      else
         delay 5
      end if
   end repeat
end tell
--continue here now that the file exists

I think the problem might be that your current test is trying to resolve the alias variable and then test for its existence, so you get the error before the test. But the above seems to work for me…

great! that seems to do the trick. However I now have a different problem. Finder seems to be unable to resolve a path that’s stored in a variable.

I think to best explain I should show my complete script:


global downloadLocation, iTunesBaseDir

property downloadLocation : ""
property iTunesBaseDir : ""

set downloadLocation to (path to desktop) as Unicode text
set iTunesBaseDir to (((path to music folder) as Unicode text) & "iTunes:iTunes Music") as Unicode text

using terms from application "Mail"
	on perform mail action with messages theMessages
		repeat with thisMessage in theMessages
			
			-- extract required info
			set theContents to content of thisMessage
			set oldDelimiters to AppleScript's text item delimiters
			
			-- get artist and album details
			set AppleScript's text item delimiters to {" request for "}
			set parseString to text items of theContents
			set parseString to item 2 of parseString
			set AppleScript's text item delimiters to {", "}
			set theArtist to text item 1 of parseString
			set theAlbum to text item 2 of parseString
			
			-- extract theURL
			set AppleScript's text item delimiters to {" at "}
			set parseString to text item 2 of parseString
			set AppleScript's text item delimiters to {return}
			set theURL to text item 1 of parseString
			
			-- extract filename
			set AppleScript's text item delimiters to {"/"}
			set parseString to text items of theURL
			set parseString to item 5 of parseString
			set AppleScript's text item delimiters to {"
"}
			set theTrack to text item 1 of parseString
			set AppleScript's text item delimiters to oldDelimiters
			set fullDestPath to (iTunesBaseDir & theArtist & ":" & theAlbum & ":") as Unicode text
			set partDestPath to (iTunesBaseDir & theArtist & ":") as Unicode text
			set fullSrcFile to (downloadLocation & theTrack) as Unicode text
			set fullDestFile to (fullDestPath & theTrack) as Unicode text
			
			-- it seems that any variable that is built using ituneBaseDir or downloadLocation
			-- is incomplete
			display dialog theTrack
			display dialog fullDestPath
			display dialog partDestPath
			display dialog fullSrcFile
			display dialog fullDestFile
			
			-- download track
			tell application "Safari"
				try
					open location theURL
				on error theError
					display dialog "Safari: " & theError
				end try
			end tell
						
			-- Check to see if artist/album dir already exists. if not then create it
			tell application "Finder"
				try
					if not (the folder fullDestPath exists) then
						if not (the folder partDestPath exists) then
							make new folder at alias iTunesBaseDir with properties {name:theArtist}
						end if
						
						make new folder at alias partDestPath with properties {name:theAlbum}
					end if
				on error theError
					display dialog "Finder" & theError     -- I always get an error here
				end try
				
				try
					repeat
						if (file fullSrcFile exists) then
							exit repeat
						else
							delay 5
						end if
					end repeat
				on error theError
					display dialog theError
				end try
				
				move item fullSrcFile to folder fullDestPath
			end tell
			
			-- delete messsage
			tell application "Mail"
				try
					set read status of thisMessage to true
					set deleted of thisMessage to true
				on error theError
					display dialog "Mail: " & theError
				end try
			end tell
			
			-- import track into iTunes
			tell application "iTunes"
				try
					add fullDestFile to playlist "Library"
				on error theError
					display dialog "iTunes: " & theError
				end try
			end tell
			
		end repeat
	end perform mail action with messages
end using terms from

display dialog "complete"

As the comments in the code say I always get an error when trying to create the path in the iTunes Music directory. The problem I see is - for example - that fullDestPath isn’t the sum of iTunesBaseDir + theArtist + theAlbum. The iTunesBaseDir part is missing.

Any idea what is going on and how to fix it?

Aha! Fixed it. :smiley:

Turns out that the global variables weren’t so global after all !

By changing the code to:


global downloadLocation, iTunesBaseDir

property downloadLocation : ""
property iTunesBaseDir : ""

using terms from application "Mail"
	on perform mail action with messages theMessages
		set downloadLocation to (path to desktop) as Unicode text
		set iTunesBaseDir to (((path to music folder) as Unicode text) & "iTunes:iTunes Music") as Unicode text
		repeat with thisMessage in theMessages

I was able to fix the problem. :slight_smile: