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?
 
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
 
mkuneshAuthor Commented:
Excellent, that worked exactly as expected.  Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.