create hidden folder

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.

Just put a “.” in front of the name.

Ex.


set theName to ".hiddenFolder"

tell application "Finder"
	make new folder at desktop with properties {name:theName}
end tell

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

You’re getting an error message because the folder already exists, which means that the folder is not being deleted.

delete would work fine, if your script actually made it to that line.

tell application "Finder"
	return exists folder ".Export"
end tell

Sorry I’m not following you. (I know I’m a noob). :slight_smile:

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)

Hi James,

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))

But maybe I got it all wrong :lol:

Ahh, so it can! Is this new in Leopard? (The functionality not the command)

Yes, it seems to be. I couldn’t find the hidden parameter on my Tiger machine :wink:

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).

BTW, John Gruber has written a bit about SetFile, chflags and other ways of marking stuff as invisible (though he does not mention the above System Events method). He also notes that chflags and SetFile are not identical in their operation on non-HFS+ filesystems. I have no data on whether set visible works more like chflags or SetFile in this regard.

Edit: Added info about and links to the Daring Fireball article.

Just a hint:

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.