Script to hide all files on the desktop

Hi, this is my first script that i made all by myself (i just looked for the command to make the files appear/disappear), I hope you enjoy it and find ways to make it better !!!

display dialog "if you want to hide the icons of the desktop continue" buttons {"Cancel", "Continue"}
set dialog1 to display dialog "click 'disappear' to hide icons, or 'appear' for them to appear" buttons {"Disappear", "Appear"}
if button returned of dialog1 = "Disappear" then do shell script "chflags hidden ~/Desktop/*"
do shell script "killall Finder"
if button returned of dialog1 = "Appear" then do shell script "chflags nohidden ~/Desktop/*"

Welcome to MacScripters.

Here is one I use for watching the earth in black! :slight_smile: (I can’t remember where I found it at the moment, but I didn’t write it. I am not sure which approach I like better, but acl’s is a nice way to achieve the hiding! :slight_smile:


# Fixed, as it doesn't have the default when it has never been used.
set a to "1"
try
	set a to do shell script "defaults read com.apple.finder CreateDesktop"
end try
if a is "1" then
	do shell script "defaults write com.apple.finder CreateDesktop -bool false"
	--display dialog "Desktop Hidden"
end if
if a is "0" then
	do shell script "defaults write com.apple.finder CreateDesktop -bool true"
	--display dialog "Desktop Revealed"
end if

tell application "Finder" to quit
delay 0.2
do shell script "open /System/Library/CoreServices/Finder.app"

or


try
	set state to ((do shell script "/usr/bin/defaults read com.apple.finder CreateDesktop") as integer) as boolean
on error
	set state to false
end try
do shell script "/usr/bin/defaults write com.apple.finder CreateDesktop -bool " & ((not state) as text) & "; killall Finder"


Hello. :slight_smile: That was much better Stefan!

I worked a little bit more on it, to make it consider Spotlight windows, so you get a warning before closing them.

It won’t compile correctly the first time, as computer-object gets changed into system-attribute-object, so you have to enter «class ccmp» to make it work, (until you edit again.).

Here it is:


script toggleDesktopIcons
	property FinderIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:Finder.app:Contents:Resources:Finder.icns")
	
	on run
		local hasSpotlightWindows, bndlIdentfier, theAnswer
		tell application id "sevs" to set bndlIdentfier to bundle identifier of the first application process whose frontmost is true and visible is true
		# saves the frontmost app, for the case that it isn't finder and we abort the operation.
		set hasSpotlightWindows to my openSpotlightWindows()
		if hasSpotlightWindows = true then
			with timeout of 301 seconds
				tell application "SystemUIServer"
					activate
					try
						set theAnswer to button returned of (display dialog "You have finder Spotlight windows open. Do you want to loose them by restarting Finder?" with title my ScriptTitle buttons {"No", "Yes"} cancel button 1 default button 1 with icon my FinderIcon giving up after 300)
					on error
						set theAnswer to "No"
					end try
				end tell
			end timeout
		else
			set theAnswer to "Yes"
		end if
		
		
		if theAnswer is "Yes" then
			try
				set state to ((do shell script "/usr/bin/defaults read com.apple.finder CreateDesktop") as integer) as boolean
			on error
				set state to false
			end try
			do shell script "/usr/bin/defaults write com.apple.finder CreateDesktop -bool " & ((not state) as text) & "; killall Finder"
			delay 1
			do shell script "open -g " & POSIX path of (path to application "Finder")
			if bndlIdentfier is not "com.apple.finder" then do shell script "open -b \"" & bndlIdentfier & "\""
			do shell script "/Applications/Utilities/Expose.app/Contents/MacOS/Expose 1"
		else
			do shell script "open -b \"" & bndlIdentfier & "\""
		end if
		
	end run
	
	on openSpotlightWindows()
		local hasSpotlightWindows
		set hasSpotlightWindows to false
		# figuring out if we have any spotlight windows
		script o # script for enhancing speed, not needed really 
			property ides : {} # ides of wins
			property targs : {} # targets
		end script
		
		tell application id "MACS" # collecting targets and ids of Finders windows, as fast as possible.
			set {o's targs, o's ides} to {name, id} of its every Finder window
			repeat with i from 1 to length of o's ides
				try
					set a to target of (its Finder window id (item i of o's ides)) as text
					# Just generating a run time error if there isn't a spotlight window, to signal that we got.
					
				on error
					if class of (target of (its Finder window id (item i of o's ides))) = computer-object then
						-- if computer-object breaks into: (system attribute -object), replace it with : «class ccmp» 
					else
						set hasSpotlightWindows to true
						exit repeat
					end if
				end try
			end repeat
		end tell
		return hasSpotlightWindows
	end openSpotlightWindows
end script
tell toggleDesktopIcons to run

I have updated the script to reveal the desktop if the appearance of the desktop has been changed.

«class ccmp» is still annoying! :slight_smile:

Hello. I mended it some more, so that it returns to the app that was active properly, before showing off the desktop.

the advantage of my script, is that you could also use it to hide the documents and any folder in there e.g…:

do shell script "chflags hidden ~/Documents/applescript/*"

i think that can make it very useful… it can easily replace any document vault, and that easily. I am trying to make it so that you choose the file you want to hide

Absolutely :slight_smile:

There were no “better” here, just another way. But if the objective is to save resources, as the Desktop I think is run as an independent process, then I think Stefan’s and mine version scores better, but not necessarily.

I just got those thoughts while I were at it, and implemented them to get them out of my head.

If you are dealing with a user that isn’t that fluent in Unix and Mac Os X, then your script either does a better job of hiding, stuff. I didn’t see that purpose, other than hide stuff visually, so that I either can see output from geek tool, and image made by omni plan, or just see the lights of the world at night. :slight_smile:

As i said, since this is my first script that i made myself, i am not very good yet.
if any of you know how to add a file browser in it that tells the POSIX path and could then choose to hide this file, i would be very happy to know how to do that :).

This should basically do the trick.


tell application "SystemUIServer"
	try
		set thf to choose file default location (path to desktop folder) with prompt "Choose a file to hide"
	on error
		error number -128
	end try
end tell

set pxPath to POSIX path of thf

Too complicated, choose file aborts the script anyway by throwing error -128 in case the user presses Cancel


set pxPath to POSIX path of (choose file default location (path to desktop folder) with prompt "Choose a file to hide")

:slight_smile: Agreed, but it is a good starting point if you are to do more in the case that the user presses Cancel.

there is only one “more”: choose file returns the alias of the chosen file

thanks for replying!!! :slight_smile:
i am working on adding one of the path finder, but not quite there yet.
thanks
Tocode