hi,
In the script below, I am trying to allow the user to enter a file name and open it if it exists in an application and display a message if the file name does not exist. I am having a problem in the second part, trying to display the message when the file name doesn’t exist. any ideas or suggestions will help, thanks.
tell application "Some application"
set text_entered to text returned of (display dialog "Please enter fine name:" buttons {"Cancel", "Open"} default button "Open" default answer "") as Unicode text
if text_entered exists then
open text_entered
else
display dialog "file name is incorrect, check the name or update your license"
end if
end tell
you should specfiy the directory you want to search in.
Without any specification either the root directory of the startup disk or
in the case of the Finder the desktop folder of the current user will be taken.
This is a way to do it:
set defaultDir to ((path to desktop as Unicode text) & "myDir:")
tell application "Some application"
set text_entered to text returned of (display dialog "Please enter fine name:" buttons {"Cancel", "Open"} default button "Open" default answer "")
try
open (defaultDir & text_entered) -- throws an error if file doesn't exist
on error
display dialog "file name is incorrect, check the name or update your license"
end try
end tell
Notes: text returned of display dialog returns Unicode text, so no coercion is needed Open [path string] works only, if the target application expects a path string
this is weird, when I try to open a file that doesn’t exist on the application, no errors are thrown!
just a note, the file not necessarily is in a folder.
‘open’ is an application command. It seems that some applications, if the file doesn’t exist, will display their own message before returning an error to the script. One way to test for the existence of a file in vanilla AppleScript is to try coercing the path to alias. This can only succeed if the file exists. It’s usually better practice to use some sort of file reference rather than just a path anyway.
set defaultDir to ((path to desktop as Unicode text) & "myDir:")
tell application "Some application"
set text_entered to text returned of (display dialog "Please enter file name:" buttons {"Cancel", "Open"} default button "Open" default answer "")
try
set the_file to (defaultDir & text_entered) as alias -- throws an error if file doesn't exist
open the_file
on error
display dialog "file name is incorrect, check the name or update your license"
end try
end tell