Is there a way to create a new/hidden folder through apple script?
Right now I’m creating new files using this script:
tell application "Finder"
if exists folder "Ink and Paint Export" then
delete folder "Ink and Paint Export" -- moves to trash
end if
make new folder with properties {name:"Images"}
end tell
The only way to make a file/folder hidden is to begin the name with a “.” or by using the SetFile terminal command with the “-V” option. SetFile is installed as part of Developer Tools.
That works great but now I cannot get the script to move the file to the trash if it exists.
Even with the if exists command I get an error message “the operation could not be completed because there is already an item with that name.”
How can I set the script to delete it if it exists?
tell application "Finder"
if exists folder ".Export" then
delete folder ".Export" -- moves to trash
end if
set thefolder to (make new folder with properties {name:".Export"})
end tell
What I’m trying to do is create a hidden folder on the desktop called “export” (or I guess .export). If the folder already exists then I want finder to delete the folder and all its contents and create a new one.
If the folder doesn’t exist then I want to create a new one.
I’ve used this script before with visible folders, and it has worked fine, so I’m not sure why it doesn’t work with invisible folder.
Sorry if I’m being a pain.
tell application “Finder”
if exists folder “.Export” then
delete folder “.Export”
end if
set thefolder to (make new folder with properties {name:“.Export”})
end tell
I got this idea from StefanK. I does the trick for me.
doErase("~/Desktop/.Export/")
on doErase(E)
try
do shell script "rm -rf " & E
end try
end doErase
tell application "Finder"
set thefolder to (make new folder with properties {name:".Export"})
end tell
Tom
Browser: iCab/4.1.1
Operating System: Mac OS X (10.4)
Developer Tools are not needed to hide a folder, /bin/chflags can do it too
set E to quoted form of (POSIX path of (path to desktop) & "Export")
do shell script "/bin/rm -rf " & E & ";/bin/mkdir " & E & ";/usr/bin/chflags hidden " & E
Just a thought that came to me while reading the discussion: Maybe just creating a new folder in the Temporary Items folder would already be sufficient?
It seems to me that Kalina basically wants to hide the script process from the user. And in my personal opinion, in this case using the temp folder would be better than creating a hidden folder:
set tmpfolderpath to (path to (temporary items folder)) as Unicode text
set expfolderpath to tmpfolderpath & "export:"
try
set expfolderalias to expfolderpath as alias
-- export folder exists, so we delete it
do shell script ("rm -rf " & quoted form of (POSIX path of expfolderpath))
end try
-- creating a fresh export folder
do shell script ("mkdir -p " & quoted form of (POSIX path of expfolderpath))
Recently I had cause to hide some volumes on a Tiger (10.4.11) machine that did not have the developer tools installed. I have had good results using the visible property of System Event’s disk items to hide (or reveal) files, folders and volumes on Tiger machines (for volumes, I have to restart Finder for the change to be effective).
Working with folders and files in the user’s tmp folder might be a good solution instead of using hidden folders/files. But there is something you will have to take care about.
For the instance you use a temporary file that holds data the script writes into, you should take care to close for access the file and maybe delete it before running your script again. Otherwise an opened file remains in the tmp folder and this causes errors when trying to write in it again on the next run.
Same happens with a hidden file. You should always catch this with any file you’ll write into on runtime. Using folders should require to check for the folder’s existence then for it’s contents, cleaning it up when your script launches if lost files are still within after a program crash.