applescript "minimize the desktop"

Discussion in 'Mac Apps and Mac App Store' started by ifjake, Feb 27, 2005.

  1. ifjake macrumors 6502a

    Joined:
    Jan 19, 2004
    #1
    i'm curious if there is a way with apple script to, in a sense, minimize my desktop, which really means, using a combination of an applescript executable and a "folder action" (if folder actions work the way i think they do) to sort of toggle the desktop on and off. running the apple script little executable thing would take all the contents of my desktop and place it in a folder on the desktop, and clicking on the folder would run another script that takes all the contents out of the folder and puts it back on the desktop.

    you can stop reading now if you want, i'm just going to babble a little bit on my use of the desktop. essentially i like using it as my workspace, putting current and recent projects on it, downloads, so that when i open up my laptop i can go right to what i need to work on or look at without sifting through a system of folders and such. i like using folders, but i'm so organized and specific that it becomes a little bit of a hunt to get to things. while it's keeps things organized and easy to find, it also takes a little bit of time and it's more efficient to keep things right on the desktop where i can find it if i'm still working on it or need quick reference or whatever. but i'd also like to keep a clean desktop at times, which is what i would like to use this apple script idea for.

    i'm guessing that this idea shouldn't be too hard to do. but i really don't know where to start. maybe spotlight will enable me to come up with a different technique. but in the mean time...
     
  2. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #2
    Firstly, make a new script from the following code:
    Code:
    on opening folder theFolder
    	tell application "Finder"
    		try
    			--Move the files back
    			move every item of theFolder to desktop without replacing
    			--Delete the now empty folder
    			delete theFolder
    		on error
    			--Probably a "file already exists" error, which we will ignore
    		end try
    	end tell
    end opening folder
    Save it as a script in /Library/Scripts/Folder Action Scripts/DesktopMaximizer.scpt (The location and name are important). This script will be run to restore the desktop. There's no need to attach it to a folder, as this is done automatically by the second script.

    The second script is as follows:

    Code:
    property minimizedFolderName : "Desktop (Minimized)" --The name of the folder where files are put
    property folderActionName : "DesktopMaximizer.scpt" --The name of the script that restores the desktop
    
    tell application "Finder"
    	--First create/get a reference to the folder we will move the files to
    	if (exists (folder minimizedFolderName of desktop)) then
    		--The folder exists, get a reference to it
    		set minimizedFolder to folder minimizedFolderName of desktop
    	else
    		--The folder doesn't exist, create it
    		set minimizedFolder to make new folder at desktop with properties {name:minimizedFolderName}
    	end if
    	
    	--Attach the folder action to the folder if necessary
    	tell application "System Events"
    		(*First delete the folder action for a folder of this name
    		if it already exists, to prevent folder actions referring
    		to a deleted folder from the previous time the
    		script was run.*)
    		if (exists folder action minimizedFolderName) then
    			delete folder action minimizedFolderName
    		end if
    		--Add the folder to the list of folders with folder actions
    		make new folder action at end of folder actions with properties {path:(minimizedFolder as alias)}
    		tell folder action minimizedFolderName
    			make new script at end of scripts with properties {name:folderActionName}
    		end tell
    	end tell
    	
    	--Get every file/folder on the desktop, except mounted items and minimizedFolder
    	set theFiles to every item of desktop whose class is not disk and name is not minimizedFolderName
    	
    	--Move the files
    	try
    		move theFiles to minimizedFolder without replacing
    	on error
    		--Probably a "file already exists" error, which we will ignore
    	end try
    end tell
    Save it as an application, with all the options in the save dialog turned off. Unlike the other script, the name and location do not matter, although you might want to save it on the desktop for convenience.

    It should now work. Run the second script and everything on the desktop (excluding disks) will be moved to a folder called "Desktop (Minimized)". Open the folder and everything will be moved back, and the folder will delete itself.

    I've tried to code it so that there shouldn't be any data loss. For example if you run the second script, then save a file to the desktop with the same name as one of the moved files, neither file will be deleted when you restore the desktop. Instead the "Desktop (Minimized)" folder will remain on the desktop with the files couldn't be moved inside it. Regardless, I'd recommend having backups of all the files on your desktop at least until you are confident that the scripts work as expected.
     
  3. jackieonasses macrumors 6502a

    jackieonasses

    Joined:
    Mar 3, 2004
    Location:
    the great OKLAHOMA....
    #3
    Let me give a public "shout-out" to HexMonkey. Dude, you are one amazing contributor to this forum. And you know more code then i know words of the English language!

    kyle
     
  4. ifjake thread starter macrumors 6502a

    Joined:
    Jan 19, 2004
    #4
    wow that's awesome. thanks a bunch. if anyone wants to try this, make sure they go to the "Folder Action Setup" in the "AppleScript" folder and check the "Enable Folder Actions" box. just one little detail that i didn't have turned on that made it look like it wasn't working. i'm going to try to break it now, see what i can get away with. ;)
     
  5. may214 macrumors newbie

    Joined:
    Jun 1, 2009
    #5
    What about including the position of restored icons?

    Love the desktop minimize/maximize applescript. Is it possible to 'remember' where the file icons and folders and restore them to that position?
     
  6. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #6
    They're not perfect, but try the following two scripts instead of the originals. The other steps are the same. It stores the file locations in an invisible file within the Desktop (minimized) folder and reads them when it restores the desktop. The standard disclaimers apply; make sure your files are backed up before trying the scripts.

    DesktopMaximizer.scpt:
    Code:
    on opening folder theFolder
    	
    	tell application "Finder"
    		--Load file locations and then delete the location data file
    		set dataFile to ((theFolder as string) & ".FileLocs.txt") as file specification
    		set fileLocations to read dataFile as list
    		delete dataFile
    		
    		--Attempt to move the files back to the desktop
    		try
    			move every item of theFolder to desktop without replacing
    			--Delete the now empty folder
    			delete theFolder
    		on error
    			--Probably a "file already exists" error, which we will ignore
    		end try
    		
    		--Attempt to move the files back to their original locations on the desktop
    		repeat with nextFile in fileLocations
    			try
    				set filePos to {x of nextFile, y of nextFile}
    				try --file
    					set theFile to file ((desktop as string) & (filename of nextFile))
    				on error --folder
    					set theFile to folder ((desktop as string) & (filename of nextFile))
    				end try
    				set desktop position of theFile to filePos
    			end try
    		end repeat
    	end tell
    end opening folder
    Desktop minimizer:
    Code:
    property minimizedFolderName : "Desktop (Minimized)" --The name of the folder where files are put
    property folderActionName : "DesktopMaximizer.scpt" --The name of the script that restores the desktop
    
    tell application "Finder"
    	--First create/get a reference to the folder we will move the files to
    	if (exists (folder minimizedFolderName of desktop)) then
    		--The folder exists, get a reference to it
    		set minimizedFolder to folder minimizedFolderName of desktop
    	else
    		--The folder doesn't exist, create it
    		set minimizedFolder to make new folder at desktop with properties {name:minimizedFolderName}
    	end if
    	
    	--Attach the folder action to the folder if necessary
    	tell application "System Events"
    		(*First delete the folder action for a folder of this name
    		if it already exists, to prevent folder actions referring
    		to a deleted folder from the previous time the
    		script was run.*)
    		if (exists folder action minimizedFolderName) then
    			delete folder action minimizedFolderName
    		end if
    		--Add the folder to the list of folders with folder actions
    		make new folder action at end of folder actions with properties {path:(minimizedFolder as alias)}
    		tell folder action minimizedFolderName
    			make new script at end of scripts with properties {name:folderActionName}
    		end tell
    	end tell
    	
    	--Get every file/folder on the desktop, except mounted items and minimizedFolder
    	set theFiles to every item of desktop whose class is not disk and name is not minimizedFolderName
    	
    	--Get file locations and store them in an invisible file
    	set dataFile to ((minimizedFolder as string) & ".FileLocs.txt") as string
    	set refNum to open for access file dataFile with write permission
    	set eof refNum to 0
    	set theLocations to {}
    	repeat with nextFile in theFiles
    		set filePos to desktop position of nextFile
    		set theRecord to {filename:(name of nextFile), x:(item 1 of filePos), y:(item 2 of filePos)}
    		set end of theLocations to theRecord
    	end repeat
    	write theLocations to refNum starting at eof as list
    	close access refNum
    	
    	--Move the files
    	try
    		move theFiles to minimizedFolder without replacing
    	on error
    		--Probably a "file already exists" error, which we will ignore
    	end try
    end tell
     

Share This Page