How to check the existence of a directory in AppleScript

I am trying to write an AppleScript to automate the usage of a separate application.  Essentially the AppleScript launches the application, delays a few seconds, activates the application, delays a few seconds, and begins entering text to fill out fields.  I want to add an "if else" statement to check the existence of a a specific directory in the user's home folder.  If the directory does not exist, I want the time delay between the application launching and the application activating to be larger.  If the directory does already exist, then I want the delay to be smaller.

I attached the beginning part of the AppleScript, all of which works fine.  I need to start off the script with some sort of "if" statement that "if directory ~/Library/Application\ Support/TextEdit does not exist, delay 5 minutes.  If it does exist, then delay 10 seconds.  After the time delays, I need it to continue with the script telling System Events to keystroke different info.


#open's the program TextEdit.app
delay 5
do shell script "open -a TextEdit"
delay 30
tell application "TextEdit"
	activate
	tell application "System Events" to keystroke "TestText"
	tell application "System Events" to keystroke tab
	tell application "System Events" to delay 0.5
	tell application "System Events" to keystroke tab
end tell

Open in new window

mkuneshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SimonCommented:
Try this code: The existence test works by attempting to assign the folder path to a variable _as an alias_ which by definition in this contect means it must exist for the command to succeed. The command is inside a try block so it if it causes an error it doesn't stop the script.
 


# Create a string version of the path to the folder whose existence we need to check
set strFolderPath to ((path to current user folder) as string) & "Library:Application Support:TextEdit:"

set bFolderExists to false -- if aliasing test fails, this value remains false

# Attempt to create an alias to the folder by assigning it to a variable. 
#This will cause a trappable error if the path doesn't point to an existing object.
try
	set objFolder to alias strFolderPath
	set bFolderExists to true -- this line not executed if the previous line causes an error
	set objFolder to nothing --release variable
end try

log "Does the folder exist? - " & bFolderExists & " -"

# Create a short delay if the folder exists or a longer one if it doesn't
if bFolderExists then
	log "delaying 5"
	delay 5
else
	log "delaying 15"
	delay 15
end if

# open the program TextEdit.app
do shell script "open -a TextEdit"
delay 30
tell application "TextEdit"
	activate
	tell application "System Events" to keystroke "TestText"
	tell application "System Events" to keystroke tab
	tell application "System Events" to delay 0.5
	tell application "System Events" to keystroke tab
	tell application "System Events" to keystroke "More Text"
end tell

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mkuneshAuthor Commented:
Excellent, that worked exactly as expected.  Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Swift Programming

From novice to tech pro — start learning today.