Change the visibility of a file or folder in the Finder

Over the last month I’ve been teaching myself applescript and I found this website very helpful so I wanted to give something back.

Here’s a script which will toggle the visibility of a file or folder in the Finder. You are presented with a dialog box to make your selection. The file/folder will have a “.” added or removed from its name to change its visibility. The location of the file/folder wil not change.


-- store the original text item delimiters and set them to "" for this script
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""

-- present a dialog to decide if you want to change the visibility of a file or folder
set tempVar to display dialog "Do you want to change the visibility of a file or folder?" with icon note buttons {"Cancel", "File", "Folder"} default button "Folder" with title "File of Folder?"
set buttonEntered to the button returned of tempVar

-- present the choose dialog for a file or folder
if buttonEntered is "Folder" then
	set theOrigPath to (choose folder with invisibles) as string
else
	set theOrigPath to (choose file with invisibles) as string
end if

-- if a folder was selected remove the ":" from the end of the string
if buttonEntered is "Folder" then
	set theChangedPath to items 1 through -2 of theOrigPath as string
else
	set theChangedPath to theOrigPath as string
end if

-- separate the name of the file/folder from the rest of the path
set AppleScript's text item delimiters to ":"
set theName to text item -1 of theChangedPath as string
set theShortPath to text items 1 thru -2 of theChangedPath as string
set AppleScript's text item delimiters to ""

-- toggle the visibility of the file/folder name by adding or removing a "."
if item 1 of theName is "." then
	set theChangedName to items 2 thru end of theName as string
else
	set theChangedName to "." & theName as string
end if

-- if a folder was selected add back the ":" to the end of the name
if buttonEntered is "Folder" then set theChangedName to theChangedName & ":" as string

-- calculate the new path with the visibility toggled for the file/folder
set theNewPath to theShortPath & ":" & theChangedName as string

-- make the path strings into unix posix paths
set origUnixPath to quoted form of POSIX path of theOrigPath
set newUnixPath to quoted form of POSIX path of theNewPath

-- change the visibility
do shell script "mv " & origUnixPath & " " & newUnixPath

-- restore the original the text item delimiters
set AppleScript's text item delimiters to tid

Model: PM dual 2.0 GHz G5
AppleScript: AppleScript 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

This is another way to toggle the visibility. My first applescript above changes the visibility by adding or removing a “.” before the file/folder name. This applescript uses the SetFile and GetFileInfo unix executables to set the visibility bit of a file or folder. So this applescript won’t actually change the name of the file/folder like the above applescript does.

Note: you must have the Developer Tools installed for this applescript to work because the Developer Tools installs the needed unix executables.


-- this will toggle the visibility bit on a file or folder
-- developer tools must be installed because you need the SetFile and GetFileInfo unix executables
-- these executables are located in /Developer/Tools

-- present a dialog to decide if you want to change the visibility of a file or folder
set tempVar to display dialog "Do you want to change the visibility of a file or folder?" with icon note buttons {"Cancel", "File", "Folder"} default button "Folder" with title "File of Folder?"
set buttonEntered to the button returned of tempVar

-- present the choose dialog for a file or folder
if buttonEntered is "Folder" then
	set macPath to choose folder with invisibles
else
	set macPath to choose file with invisibles
end if

-- get the unix path of the choice
set unixPath to POSIX path of macPath

-- check for the visibility so you can toggle it
set attributes to do shell script "/Developer/Tools/GetFileInfo " & quoted form of unixPath & "| grep attributes"

-- toggle the visibility
considering case
	if attributes contains "v" then
		do shell script "/Developer/Tools/SetFile -a V " & quoted form of unixPath -- v means visible, so make it invisible
	else
		do shell script "/Developer/Tools/SetFile -a v " & quoted form of unixPath -- V means invisible so make it visible
	end if
end considering

-- restart the finder
do shell script "killall Finder"

Thank you so much for the scripts! Well, what I would like to do is to add via Automator a Finder contextual to make files/folder visible or invisibile via v/V flag, because I prefere tu customize OSX via Automator/scripts than using 3rd party apps.

The second script of yours is REALLY great, for making you selecting your file/folder via a choose windows showing (with) invisibles files/folders, it basically makes redundant the use of another script to 'make all files visible in your Finder view0 (<- very common) when you want to made visibile again (or just open) a previosly-made-invisible file. Very, very handy.

BUT there’s something I fail to understand.

  1. Why folders and files got separate choose windows? Isn’t it possible to have just one to select any kind of item (file or folder) to toggle its visibility flag?

  2. It’s sad the choose windows always prompts selection from root. Wouldn’t be possible to retrieve a path of a selected file in finder and directly prompting the choose window with that one selected? That would save quite a lot of times! Of course, with no file selected in finder, choose window should prompt starting selection from the root.

  3. Would it be possible to allow multiple selections, and particularly the function to ‘toggle visibility flag for a folder and ALL it contents’?

Thanks in advance for you answer and time!

Operating System: Mac OS X (10.4)

File or Folder is just the way AppleScript is set up.

Here’s a script by Kai Edwards that will permit a choice of either:

to |list items|(list_title, list_content)
	set text item delimiters to return & tab
	paragraphs of ((list_title as Unicode text) & return & tab & list_content)
end |list items|

to |choose folder or file| from folder_alias
	set tid to text item delimiters
	set posix_path to folder_alias's POSIX path
	set item_list to {}
	tell application "Finder" to tell folder folder_alias
		if (count folders) > 0 then set item_list to item_list & my |list items|("Choose Folder:", name of folders)
		if (count files) > 0 then set item_list to item_list & my |list items|("Choose File:", name of files)
	end tell
	set text item delimiters to tid
	if (count item_list) is 0 then return |choose folder or file| from choose folder with prompt ¬
		"There are no items in " & posix_path & ". Please choose another folder:" default location folder_alias
	set l to choose from list item_list with prompt "Choose from " & posix_path & ":" OK button name ¬
		"Select" cancel button name "Change Folder" with multiple selections allowed and empty selection allowed
	if l is false then return |choose folder or file| from choose folder default location folder_alias
	set text item delimiters to ""
	set l to l as Unicode text
	repeat with i in {"Choose Folder:", "Choose File:"}
		set text item delimiters to i
		if (count l's text items) > 1 then
			set l to l's text items
			set text item delimiters to ""
			set l to l as Unicode text
		end if
	end repeat
	set text item delimiters to tab
	set l to rest of l's text items
	set text item delimiters to tid
	tell application "Finder" to tell folder folder_alias
		if (count l) is 1 then return (first item whose name is in l) as alias
		(items whose name is in l) as alias list
	end tell
end |choose folder or file|

|choose folder or file| from path to documents folder (* or any other folder alias *)

Guyz. I’m such a scrub. That’s way beyond me… :frowning:

I hope the author of the original two script in this thread may read this and be kind enough to spent some time in editing his script according to my need s (IF possible). Guess my eternal gratitude would not be that much as a reward, but who knows, a man can dream can’t he? :slight_smile:

On top of that, I wonder if is possible to use the needed DevTools whitout installing the whole package (it’s almost 1gig and I just need those two executable Set File and GetFileInfo. I tried to have 'em sent to me by a friend of mine, but just putting them in the supposed directory isn’t working for some permission errors… :frowning:

Adam’s suggestion gave me an idea of how to present files and folders at the same time. I present them as either invisible or visible items (instead of files and folders). I think it works well for what Shito wanted. As such this new script addresses Shito’s #1 and #2 questions. It also allows multiple selections. It doesn’t address “ALL contents” though because it would add too much complexity to the script.

I think you can install them without installing the whole developer package, but I’m not sure. 2 things to check. 1) make sure the proper path to where you put those files is listed in this script. Set their path in the variables unix_path_to_GetFileInfo and unix_path_to_SetFile at the top of this script. 2) I checked the permissions on my SetFile and GetFileInfo executables. You can try setting your permissions to what mine are. Open a Terminal window and change to the directory where you have the executables stored… then run these commands.

sudo chmod u=rx,g=rx,o=rx SetFile
sudo chown root:wheel SetFile
sudo chmod u=rx,g=rx,o=rx GetFileInfo
sudo chown root:wheel GetFileInfo

I hope this helps… :smiley:

-- this will toggle the visibility bit on files or folders
-- this script will present you with a list of items from the folder of the currently selected item in the Finder (or the last location used if nothing is selected)
-- the list is organized into invisible and visible items
-- from the list you can select one or more items and their visibility will be toggled
-- if nothing is selected from the list when you press the button "Toggle Visibility" then the script will exit without doing anything

(* NOTE: developer tools must be installed because you need the SetFile and GetFileInfo unix executables. Those files are normally installed in /Developer/Tools/. If you changed their location then make sure the correct path to those files is set in the first 2 variables of this script. *)


property unix_path_to_GetFileInfo : "/Developer/Tools/GetFileInfo"
property unix_path_to_SetFile : "/Developer/Tools/SetFile"
property starting_folder : path to desktop folder from user domain


-- get the folder of the selected item
tell application "Finder"
	try
		set starting_folder to (item 1 of (get selection))
	on error
		set starting_folder to starting_folder
	end try
	if kind of starting_folder is not "Folder" then set starting_folder to container of starting_folder
end tell

repeat
	-- get a list of the items in the folder including invisible files
	set starting_folder to starting_folder as alias
	set folderList to list folder starting_folder
	
	-- find which folder items are invisible and visible and put them in separate lists
	set visItems to {}
	set invisItems to {}
	repeat with anItem in folderList
		set unixPath to (POSIX path of starting_folder) & anItem
		set attributes to do shell script quoted form of unix_path_to_GetFileInfo & " " & quoted form of unixPath & "| grep attributes"
		considering case
			if attributes contains "v" then
				set end of visItems to anItem as Unicode text -- v means visible
			else
				set end of invisItems to anItem as Unicode text -- V means invisible
			end if
		end considering
	end repeat
	
	-- combine the visible and invisible lists
	set formattedList to {"Invisible Items"}
	set theCount to count of invisItems
	if theCount is 0 then
		set end of formattedList to ""
	else
		repeat with i from 1 to (count of invisItems)
			set end of formattedList to (tab & item i of invisItems)
		end repeat
	end if
	set end of formattedList to "Visible Items"
	set theCount to count of visItems
	if theCount is 0 then
		set end of formattedList to ""
	else
		repeat with i from 1 to (count of visItems)
			set end of formattedList to (tab & item i of visItems)
		end repeat
	end if
	
	-- present the choose dialog to get the list of items to toggle
	set unix_starting_folder to POSIX path of starting_folder
	set thePrompt to "Items in the folder" & return & unix_starting_folder
	set toggle_items to choose from list formattedList with title "Pick one or more items" with prompt thePrompt OK button name "Toggle Visibility" cancel button name "Choose Another Folder..." with multiple selections allowed and empty selection allowed
	if toggle_items is false then
		set starting_folder to choose folder default location starting_folder
	else
		if toggle_items is {} then
			display dialog "Nothing was selected so nothing was changed!" buttons {"OK"} default button 1
			return
		else
			exit repeat
		end if
	end if
end repeat

-- toggle the visibility of the chosen items
repeat with anItem in toggle_items
	-- get the unix path of the choice
	set unixPath to (POSIX path of starting_folder) & (items 2 thru end of anItem as Unicode text)
	try
		-- check for the visibility so you can toggle it
		set attributes to do shell script quoted form of unix_path_to_GetFileInfo & " " & quoted form of unixPath & "| grep attributes"
		
		-- toggle the visibility
		considering case
			if attributes contains "v" then
				do shell script quoted form of unix_path_to_SetFile & " -a V " & quoted form of unixPath -- v means visible, so make it invisible
			else
				do shell script quoted form of unix_path_to_SetFile & " -a v " & quoted form of unixPath -- V means invisible so make it visible
			end if
		end considering
	end try
end repeat

-- restart the finder so your changes are seen
do shell script "killall Finder"

Man, are you kidding?

This is not like ‘helping’ me… this SAVED my day, month, year and most likely whole Mac computing experience. :slight_smile:

I simply have no words to thank you! Not only you created a solution for all my needs, but you also spared me the burden of having 1gig of DevTools installed (yes your custom permission inputs do work). I have now both of your latest scripts nicely fitting in the Automator subfolder of my right-click contextual menu in Finder… WOW! This is like magic to me! I went to some helper who created a custom add-on for OSX out of my personal suggestion and needs, can you imagine?

THANK YOU SO MUCH!

You rock beyond my imagination, that is.

Model: G4 Cube - Mac mini
Browser: Safari 523.12
Operating System: Mac OS X (10.4)

You’re very welcome. I actually like the changes and now I use it too.

This is my latest script. It uses a third method to change visibility. I didn’t know it until member Tom_X pointed it out in another thread, but System Events can change the visibility. So here’s the latest, cleaned up a bit and using System Events.

-- this will toggle the visibility bit of files and folders
-- this script will present you with a list of items from the folder of the front window in the Finder (or the last location used if there is no Finder window)
-- the list is organized into invisible and visible items
-- from the list you can select one or more items and their visibility will be toggled
-- if nothing is selected from the list when you press the button "Toggle Visibility" then the script will exit without doing anything


property starting_folder : path to desktop folder from user domain

-- set the starting place for the dialog window to the place of the front finder window
try
	tell application "Finder" to set starting_folder to folder of window 1
end try

repeat
	-- get a list of the items in the folder including invisible files
	try
		set folderList to list folder starting_folder
	on error
		set starting_folder to path to desktop folder from user domain
		set folderList to list folder starting_folder
	end try
	
	-- find which folder items are invisible and visible and put them in separate lists
	set visItems to {}
	set invisItems to {}
	repeat with anItem in folderList
		set thisItem to ((starting_folder as text) & anItem) as alias
		tell application "System Events" to set isVis to visible of thisItem
		if isVis then
			set end of visItems to anItem
		else
			set end of invisItems to anItem
		end if
	end repeat
	
	-- combine the visible and invisible lists
	set formattedList to {"Invisible Items"}
	set theCount to count of invisItems
	if theCount is 0 then
		set end of formattedList to ""
	else
		repeat with i from 1 to (count of invisItems)
			set end of formattedList to (tab & item i of invisItems)
		end repeat
	end if
	set end of formattedList to "Visible Items"
	set theCount to count of visItems
	if theCount is 0 then
		set end of formattedList to ""
	else
		repeat with i from 1 to (count of visItems)
			set end of formattedList to (tab & item i of visItems)
		end repeat
	end if
	
	-- present the choose dialog to get the list of items to toggle
	set unix_starting_folder to POSIX path of starting_folder
	set thePrompt to "You can choose more than one item." & return & "If you choose nothing then the script will quit."
	tell application "Finder"
		activate
		set toggle_items to choose from list formattedList with title unix_starting_folder with prompt thePrompt OK button name "Toggle Visibility" cancel button name "Choose New Folder..." with multiple selections allowed and empty selection allowed
		if toggle_items is false then
			set starting_folder to choose folder default location starting_folder
		else
			if toggle_items is {} then
				return -- end the script
			else
				exit repeat
			end if
		end if
	end tell
end repeat

-- toggle the visibility of the chosen items
repeat with anItem in toggle_items
	try
		set macPath to ((starting_folder as text) & (text 2 thru end of anItem)) as alias
		tell application "System Events"
			set theVis to visible of macPath
			if theVis then
				set visible of macPath to false
			else
				set visible of macPath to true
			end if
		end tell
	end try
end repeat