the error occurs because the Finder tries to create a folder at a literal string instead of a folder object.
By the way, the HFS equivalent of “/Volumes/Students/” is “Students:”
Another problem is to declare properties for the current user and the user folder.
The initial value of properties is set at compile time and persist under certain circumstances.
The shell command mkdir can create the folder and set the privileges at the same time
property ShareFolder : "Students"
mount volume "afp://dsc-macsrv/" & ShareFolder
tell application "System Events"
set currentUser to name of current user
set currentUserFolderExists to exists folder currentUser of folder ShareFolder
end tell
if not currentUserFolderExists then
do shell script ("mkdir -m 755 " & quoted form of ("/Volumes/" & ShareFolder & "/" & currentUser))
end if
Very handy!
I rewrote a tad, but I don’t need to report the folder exists, just if it doesn’t, create, if it does, exit script.
How do I suppress the :File Exists" error? Searching this forum I see complex fixes.
-NEWB
property MVolume : "afp://Files/"
--server volume to mount
property ShareFolder : "Docs/Visits"
--specific area to work with
mount volume MVolume & ShareFolder
tell application "System Events"
set TheFolder to "fieldinDB"
set TheFolderExists to exists folder TheFolder of folder ShareFolder
end tell
if not TheFolderExists then
do shell script ("mkdir -m 755 " & quoted form of ("/Volumes/" & ShareFolder & "/" & TheFolder))
end if
If you’re already in bash why not checking if folder exists there?
property MVolume : "afp://Files/"
--server volume to mount
property ShareFolder : "Docs/Visits"
--specific area to work with
mount volume MVolume & ShareFolder
set subFolder to quoted form of "/Volumes/" & ShareFolder & "/fieldInDB"
do shell script "[[ -d " & subFolder & " ]] || mkdir -m 755 " & subFolder
Not sure what you mean, but I’ll play to see what your code does.
I did find that once I inserted the AS code I made into my database, it didn’t treat “file exists” dialog box as a real error, so nothing is shown to user. It just creates the folder if missing.
EDIT: 2016-04-04 12:31 CT Please see my recap at the bottom of this thread.
It provides my final conclusion after much discussion in this thread.
NOTE: The below script contains an error (later corrected by DJ).
DJ, that looks like a very powerful shell script. Thanks for sharing.
When you have a few moments, would you mind detailing how it works for those of us that are bash challenged?
first the && operator wrong I’ve updated the post and it should be an or-operator “||”. In Bash every command exits with a number and zero is considered true in bash the rest is false. Like in AppleScript, with the and (and or) operator you can control a workflow as long as the return value is a boolean. For instance here a small example with two handlers.
on fileExists(thePath)
tell application "System Events" to return exists of disk item thePath
end fileExists
on createFolder(thePath)
display dialog "folder should be created"
return true
end createFolder
To create a folder as in this topic you can write it like this:
if not fileExists(theFolder) then
createFolder(theFolder)
end if
But I can also write it down as:
fileExists(theFolder) or createFolder(theFolder)
Because the return value of each command can always be interpreted as boolean in bash I can do the same there. ‘[[ -d ]]’ is an bash built-in function ([1]man page 25 “conditional expressions”) to test if a file exists and is a directory. Depending if the command returns true or false mkdir command is called, with the or (||) operator it called when the left command returns false with the and (&&) operator it’s called when the left command returns true.
[1]run the command below to open the bash manual in PDF format (quite easier to read than in Terminal).
do shell script "man -t bash | open -f -a Preview"
The first part of the bash script determines IF the subFolder already exists:
The “-d” is a switch for:
Next you have:
which is logical OR
That confuses me. Why wouldn’t you use a logical NOT before the test for existing subFolder, if we want to create the folder ONLY if it does NOT exist?
For now, I’ll move on to the next part:
The “-m 755” was unclear to me. After quite a bit of digging, I found this in the “mkdir” manual:
and then in the “chmod” manual:
So, I think I understand all of it except for the use of logical OR instead of logical NOT in the first expression. Can you add any clarification to this?
DJ did explain it, but it can be confusing at first.
With logical OR, only one of the subconditions needs to be true for the entire proposition to be true. Most computer languages (possibly all) test OR’d conditions in the order they’re presented until one of them turns out to be true or the last test returns its result. If a true result comes up, there’s no need to test any of the following conditions, so true is returned for the entire OR’d group at that point. Only if a condition proves false is the next test (if any) carried out.
The bash trick here is a sleight-of-logic based on this. If the folder exists, the test for it returns true and the script doesn’t bother executing the statement after the OR (||). If the folder doesn’t exist, the false result causes the script to try again with what it believes is another test, but which is in fact the creation of the folder. This also returns a true or false result in bash, so the script doesn’t get confused.
Conversely, with logical AND (&&), conditions are tested in the order presented until one of them turns out to be false.
But why bother checking if the folder exists (or doesn’t) at all? As I said somewhere above, if you use mkdir’s -p option, it’ll create the folder if it doesn’t exist and not error if it does.
There is actually one difference between this and checking for the folder’s existence first which could be important. Using the -p and -m options together imposes the specified permissions even where the folder already exists with different permissions. You may not always want this.
I wouldn’t say more obvious, I would say it’s more common. It’s personal preference because the if-statement cannot be written down like you wrote it down. In bash an if-statement would look like
[format]if [[ ! -d theFolder ]]
then
mkdir.
fi
[/format]
if you want to write it down as compressed as possible it would look like:
[format]if [[ ! -d theFolder ]];then
mkdir.;fi[/format]
So I think that especially compared to the last compressed example that using OR is more “obvious” than using NOT.
Update: here another example to check if a file exists and return an to boolean coercible string.
set fileExists to (do shell script "if [[ -e " & quoted form of theFile & " ]]
then
echo yes
else
echo no
fi") as boolean
in a more compressed way:
set fileExists to (do shell script "if [[ -e " & quoted form of theFile & " ]]; then
echo yes; else
echo no; fi") as boolean
Or when using the AND and OR operator:
set fileExists to (do shell script "[[ -e " & quoted form of theFile & " ]] && echo yes || echo no") as boolean
To me the last one with the AND and OR operator is the easiest to read. But like I said, it’s more like a personal preference rather than one being better than the other.
Nigel, you seem to be feeding us one partial piece of critical information at time.
If I had known when you first suggested using the -p switch with mkdir that it changes permissions on existing directory, then I would NOT have considered it at all.
Knowing that now eliminates using your suggestion.
I appreciate your help, but in the future if there are potentially adverse side effects to a suggestion you are making please tell us about it up front. This especially applies to lesser known scripts like shell scripts. Had I not pursued many follow-up questions about mkdir, I, and probably many other readers, would have not realized this issue.
IMO, using the (A OR B) syntax to execute B only IF A is false is a more advanced technique, and may not be easily recognized by a large number of scripters.