Repeat until exists never changes from false to true, despite the file existing

I want to wait until a file exists before I ask Preview to open it. My repeat until loop is not finding the file; always false. Why?

Here is some test code where the file actually already exists, in the designated location, but the repeat until loop never finds it.

When I comment out the repeat until loop, Preview opens the file using the same location variable.

set FileLocation to "/Users/john/Library/Mobile Documents/com~apple~CloudDocs/Downloads/Journaling for minimalists the next best thing and commitments.pdf"

tell application "Finder"
repeat until exists POSIX file FileLocation
	delay 1
end repeat
end tell

tell application "Preview"
    open POSIX file FileLocation
end tell

exists keeps returning “false”

But, when I comment out the loop, Preview opens the file:

Try this:

set theFile to "/Users/john/Library/Mobile Documents/com~apple~CloudDocs/Downloads/Journaling for minimalists the next best thing and commitments.pdf" as «class furl»
tell application "Finder"
	repeat until exists theFile
		delay 0.01
	end repeat
	tell application "Preview"
		open theFile
	end tell
end tell

I recommend to use a relative (HFS) path

set FileLocation to (path to library folder from user domain as text) & "Mobile Documents:com~apple~CloudDocs/Downloads:Journaling for minimalists the next best thing and commitments.pdf"
tell application "Finder"
    repeat until exists file FileLocation
       delay 1
    end repeat
end tell

tell application "Preview"
    open alias FileLocation
end tell

Otherwise try to coerce the POSIX file to alias

repeat until exists (POSIX file FileLocation as alias)

There’s a lot of ways to do this, but I would avoid the use of the Finder.

set fileLocation to "/Users/Robert/Desktop/Test File.pdf" -- user change
set fileLocation to POSIX file fileLocation

set fileCheckRepeat to 10 -- user set
repeat with i from 1 to fileCheckRepeat
	try
		set theFile to alias fileLocation
		exit repeat
	end try
	delay 0.1 -- user set
end repeat

if i = fileCheckRepeat then
	display alert "The file was not found"
	error number -128
end if

tell application "Preview"
	open theFile
end tell

Basically, the Finder doesn’t understand the posix file specifier. You can either use it before the tell block …

set FileLocation to "/Users/john/Library/Mobile Documents/com~apple~CloudDocs/Downloads/Journaling for minimalists the next best thing and commitments.pdf"
set theFile to POSIX file FileLocation

tell application "Finder"
	repeat until theFile exists
		delay 1
	end repeat
end tell

… or, less efficiently in a repeat, use it as a coercion instead:

set FileLocation to "/Users/john/Library/Mobile Documents/com~apple~CloudDocs/Downloads/Journaling for minimalists the next best thing and commitments.pdf"

tell application "Finder"
	repeat until (FileLocation as POSIX file) exists
		delay 1
	end repeat
end tell

I hatefully recommend not checking the existence of a file/folder, neither using the “Posix file” specifier, nor using the “as Posix File” coerction, since the result will always be TRUE.

To verify the existence of a file/folder, use the “alias” specifier or “as alias” coercion:

set FileLocation to "/Users/john/Library/Mobile Documents/com~apple~CloudDocs/Downloads/Journaling for minimalists the next best thing and commitments.pdf"

repeat
	try
		delay 1
		set theAlias to FileLocation as POSIX file as alias
		exit repeat
	end try
end repeat

tell application "Preview" to open theAlias

Thanks, KniazidisR. I’m not seeing that here in Mojave, but this is quite an old system now and I wouldn’t be surprised if a few things have changed in the meantime! :slightly_smiling_face:

Thank you, Nigel. I ended up using your solution because it fit more easily into my larger script and I am more comfortable with this style of method.

For anyone who is interested, this is my complete script for Ventura 13.2. It Prints to PDF (I have my reasons) an email and then opens that PDF in Preview. My use case is highlighting/marking-up the content for my info or for a response at a later date. I have a similar script that works on Safari web pages.

# https://0x7df.github.io/scripting-safaris-export-to-pdf-function-on-a-mac.html
set SaveFolderPath to "/Users/john/Library/Mobile Documents/com~apple~CloudDocs/Downloads/"
-- set SaveFolderPath to "~/Desktop/"
tell application "Mail" to activate
tell application "System Events"
tell process "Mail"
	
	click menu item "Print…" of menu "File" of menu bar 1
	
	repeat until exists sheet 1 of window 1
		
	end repeat
	
	tell group 2 of splitter group 1 of sheet 1 of window 1
		click button 1
		-- click menu item "Save as PDF" of menu 1 of menu button "PDF"
	end tell
	repeat until exists button "Save" of sheet 1 of sheet 1 of window 1
		delay 0.02
	end repeat
	set SaveAs to value of text field 1 of sheet 1 of sheet 1 of window 1
	set SaveAsCleaned to my replace_chars(SaveAs, "/", "")
	set value of text field 1 of sheet 1 of sheet 1 of window 1 to SaveAsCleaned
	
	keystroke "g" using {command down, shift down}
	repeat until exists text field 1 of sheet 1 of sheet 1 of sheet 1 of window 1
		delay 0.02
	end repeat
	tell sheet 1 of sheet 1 of sheet 1 of window 1
		set value of text field 1 to SaveFolderPath
		keystroke return
	end tell
	repeat until exists button "Save" of sheet 1 of sheet 1 of window 1
		delay 0.02
	end repeat
	click button "Save" of sheet 1 of sheet 1 of window 1
	
end tell
end tell

set SaveFilePath to SaveFolderPath & SaveAsCleaned & ".pdf"
set FileLocation to POSIX file SaveFilePath

tell application "Finder"
repeat until FileLocation exists
	delay 0.2
end repeat
end tell

tell application "Preview"
open file FileLocation
end tell

on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

Thank you, ionah. I’m always curious about this double-arrow syntax, but I have not yet looked into what it means: «class furl»

Thanks, Stefan. I already had the path working and so I did not want to mess around with using colons. I only kind of understand how colons vs slashes work.

Thanks, peavine. My goal was to wait until a file that I expected to shortly exist, does exist, and not to error out. I’m sorry that I wasn’t more clear.

Thank you, KniazidisR. I chose to stick with my Finder file references because I’m none too sure (yet) about the ins-and-outs about AppleScript and Finder. I know AppleScript is quite old, but I’m always puzzled/annoyed at how arcane the interaction seems.

Here is a way to test if file exists without calling an outside program.
It uses aliases instead and traps the error

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set myPath to (POSIX file "/Users/john/Library/Mobile Documents/com~apple~CloudDocs/Downloads/") as text
try
	alias myPath
on error errMsg number errNum -- file doesn't exist
	display alert errMsg & return & "Error #:" & errNum
end try

Here is your script from the beginning , using my method

set FileLocation to "/Users/john/Library/Mobile Documents/com~apple~CloudDocs/Downloads/Journaling for minimalists the next best thing and commitments.pdf"
set myHFSPath to (POSIX file FileLocation) as text
repeat
	 try
		 alias myHFSPath
		 exit repeat
	 on error -- file doesn't exist
		 delay 1
	 end try
end repeat

tell application "Preview"
    open POSIX file FileLocation -- posix  version
    -- or open file myHFSPath -- HFS version
end tell

**EDIT - I’m such a dunce. I didn’t see Peavine’s response already had this method.

In my experience, these problems with the exists keyword usually go away when wrapping the statement in parentheses.

Like:
if (exists file “Path:to:file”) then beep