Tuesday, July 22, 2014

#1 2012-12-15 03:23:21 am

tocode
Member
Registered: 2012-12-14
Posts: 5

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

Applescript:

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/*"

Offline

 

#2 2012-12-15 04:53:04 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2254
Website

Re: Script to hide all files on the desktop

Welcome to MacScripters.

Here is one I use for watching the earth in black! 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! smile

Applescript:


# 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"


Filed under: Hide Desktop

Offline

 

#3 2012-12-15 05:54:46 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10454
Website

Re: Script to hide all files on the desktop

or

Applescript:


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"


regards

Stefan

Offline

 

#4 2012-12-15 09:33:46 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2254
Website

Re: Script to hide all files on the desktop

Hello. 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:

Applescript:


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

Last edited by McUsrII (2012-12-15 01:01:42 pm)


Filed under: desktop, icons, toggle

Offline

 

#5 2012-12-15 11:18:17 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2254
Website

Re: Script to hide all files on the desktop

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

«class ccmp» is still annoying! smile


Filed under: desktop, icons, toggle

Offline

 

#6 2012-12-15 01:02:36 pm

McUsrII
Member
Registered: 2012-11-20
Posts: 2254
Website

Re: Script to hide all files on the desktop

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


Filed under: Finder, desktop, icons, toggle

Offline

 

#7 2012-12-16 09:55:14 am

tocode
Member
Registered: 2012-12-14
Posts: 5

Re: Script to hide all files on 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..:

Applescript:

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

Offline

 

#8 2012-12-16 01:18:38 pm

McUsrII
Member
Registered: 2012-11-20
Posts: 2254
Website

Re: Script to hide all files on the desktop

tocode wrote:

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

Last edited by McUsrII (2012-12-16 01:39:11 pm)


Filed under: desktop, toggle, icons.

Offline

 

#9 2012-12-16 02:16:02 pm

tocode
Member
Registered: 2012-12-14
Posts: 5

Re: Script to hide all files on the desktop

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

Offline

 

#10 2012-12-16 02:24:48 pm

McUsrII
Member
Registered: 2012-11-20
Posts: 2254
Website

Re: Script to hide all files on the desktop

This should basically do the trick.

Applescript:


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

Last edited by McUsrII (2012-12-16 02:30:58 pm)


Filed under: choose file, posix file

Offline

 

#11 2012-12-16 02:33:16 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10454
Website

Re: Script to hide all files on the desktop

McUsrII wrote:

Applescript:


tell application "SystemUIServer"
   set thf to missing value
   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

Applescript:


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

Last edited by StefanK (2012-12-16 02:33:33 pm)


regards

Stefan

Offline

 

#12 2012-12-16 02:38:57 pm

McUsrII
Member
Registered: 2012-11-20
Posts: 2254
Website

Re: Script to hide all files on the desktop

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

Last edited by McUsrII (2012-12-16 02:39:32 pm)


Filed under: choose file

Offline

 

#13 2012-12-16 02:43:56 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10454
Website

Re: Script to hide all files on the desktop

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


regards

Stefan

Offline

 

#14 2012-12-17 09:03:44 am

tocode
Member
Registered: 2012-12-14
Posts: 5

Re: Script to hide all files on the desktop

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

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.038 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)