My second day with Applescript is going generally ok but I have hit some issues.
I am trying to determine if a folder exists before trying to create one but missing something in the process.
My attempt is below.
property homeFolder : "Macintosh HD:Users:Dayo:Documents:MyDocs:" as alias
tell application "Finder"
set myPrefix to "ABC" as string
set mySuffix to "000" as string
set temp_name to myPrefix & "_" & mySuffix as string
if (exists folder temp_name of homeFolder) is true then
set temp_name to myPrefix & "_" & myNewSuffix as string
end if
set targetFolder to (make new folder at homeFolder with properties {name:temp_name})
end tell
It seems the if statement is not registering.
I think it has to do with how I have set up temp_name as a string but not sure
well, I seem to be having a similar problem, I’m trying to determine if a file in ~/Library/Caches/ exists, and if it does delete it.
currently, I have
set folder_to_delete to ((path to library as text) & "Caches:com.deleteme")
tell application "Finder"
display dialog folder_to_delete
if exists folder folder_to_delete then
move folder_to_delete to trash
end if
end tell
However, “library” on line 1 refers to /Library/ instead of ~/Library/. I want this script to be portable, so hardcoding the path is a no-no.
Any idea as to what I’m doing wrong?
Model: MacBook Air 2.13GHz
Browser: Firefox 3.6.10
Operating System: Mac OS X (10.6)
Actually the correct syntax is path to library folder.
The default setting is the library folder of the local domain (/Library).
Fortunately path to has the parameter from to specify the domain of the path
set folder_to_delete to ((path to library folder from user domain as text) & "Caches:com.deleteme")
Another way doing the same thing but without using the finder (script works when Finder is not running or hanging).
set homeFolder to path to home folder as string
set myPrefix to "ABC"
set mySuffix to "000"
set tempFolder to homeFolder & myPrefix & "_" & mySuffix as string
if not fileExists(POSIX path of tempFolder) then
do shell script "mkdir " & quoted form of POSIX path of tempFolder
end if
on fileExists(posixPath)
return ((do shell script "if test -e " & quoted form of posixPath & "; then
echo 1;
else
echo 0;
fi") as integer) as boolean
end fileExists
EDIT: the test -e checks if a file exists if you change this to test -d it will check if it’s a folder/directory too.
This is quite complicated. The -p switch of mkdir can create non-existing directories directly
set myPrefix to "ABC"
set mySuffix to "000"
do shell script "mkdir -p " & quoted form of (POSIX path of (path to home folder) & myPrefix & "_" & mySuffix)
Here is another interesting way to check if a file or folder exists (without tellling Finder). It takes Mac-like paths (as strings) as input:
--c-- CheckExistence(FileOrFolderToCheckString)
--d-- Check if a file or folder exists.
--a-- FileOrFolderToCheckString : string -- mac-like file or folder path
--r-- boolean -- true if exists
--x-- CheckExistence("Mac:Users:usrName:Desktop:test.txt") --> true or false
--u-- found somewhere on the internet
on CheckExistence(FileOrFolderToCheckString)
try
alias FileOrFolderToCheckString
return true
on error
return false
end try
end CheckExistence