I’m trying to test for the existence of a folder within another folder, but don’t know the correct syntax. Applescript language guide search did not clarify. Here’s what I tried:
Any help appreciated.
OK, got this much but why didn’t the above work while this did?
Thanks for reply. Finder will always need Apple paths format or can unix style be used?
In the second example, what does the “-d” prefix tell the shell? I see no conditional statement for returning true and false. Does the shell always return a boolean with the existence status of a file being referenced?
While surfing for some Applescript info recently I came upon a relevant technique involving either aliases or links which I just barely skimmed, as it wasn’t what I had searched for. What I remember is that one can create a link or alias to a file that does not exist by coercing the (possibly nonexistent) path to an alias(?) or link(?). Pretty sure it was an alias. “Exists” can then be used to test existence. Make sense or am I garbling it?
You’re welcome.
As far as I know the Finder will only work with Apple (HFS) paths. You could use system events because as far as I know it works with both. But you can use posix file if your variable contains an unix style path and you need an HFS path.
[ ] means in bash that there is an conditional expression that returns 1 or 0 that can be used in an if-then statement. The && means ‘and’ and || means ‘or’ in bash and responds to the return value of the conditional expression. So what I’m doing is making an ternary-like operator.
[ conditional expression ] && true || false
-d is an option that can be used to check if the file exists and is an directory inside an conditional expression. When I use option -e it just checks if the file exists (see manual page of test for all options that can be used in conditional expressions).
Not garbling. An alias cannot be made to an non-existence file. Which means when I coerce an path into an alias and an error will be thrown, the file doesn’t exists. I don’t like try-catch blocks but it’s more of an personal like rather than technically correct or not. I think you’re looking for something like this:
set thePath to (path to desktop folder as string) & "testfile.txt"
try
thePath as alias
set fileExists to true
on error
set fileExists to false
end try
EDIT: I can understand the confusion of how bash actually works but I can show it to you with an AppleScript example:
conditionalTest() and displayTrue() or displayFalse()
on conditionalTest()
return false
end conditionalTest
on displayTrue()
display dialog "yes"
return true
end displayTrue
on displayFalse()
display dialog "false"
return false
end displayFalse
When conditionalTest() is set to true, the displayFalse() function won’t be called. When you set the return value in conditionalTest() to false, the displayTrue() function won’t be called. It’s simulating an ternary operator this way. The drawback is that the called functions (handlers) both need to return booleans. Unlike real ternary operators, you can’t set variables unless they’re booleans.