Special Folders

I have been going through the book AppleScript 1-2-3 book by Sal Soghoain and Bill Cheeseman and in Lesson 5 it talks about special folder codes (i.e. pdoc for pictures folder of current user, mdoc for movies folder of current user etc.)

I would like to use one of those codes to set a path that works on other computers is it possible to do something similar to this


(*
Use this code to first find out what the name of your displays are first.

tell application "System Events"
	get display name of every desktop
end tell
*)

tell application "System Events"
	set theDesktops to a reference to (every desktop whose display name is "Cinema HD")
	set picture of item 1 of theDesktops to file "pdoc:SwitchResX:Desktop 5A"
end tell
tell application "System Events"
	set theDesktops to a reference to (every desktop whose display name is "Cinema HD")
	set picture of item 2 of theDesktops to file "Home Folder:Pictures:SwitchResX:Desktop 5B.jpg"
end tell

I will have a folder named “SwitchResX” and a picture named “Desktop 5A” (or “Desktop 5B”).

From what I found in another thread the following looks like the way to do it. It is hard for me to follow and I am not sure how to implement it. I am hoping to find something easier to remember, but I am guessing there might not be a way since I might have to define if it is System domain, Local domain, User domain or Network domain.

set pic_folder to (path to "pdoc" from user domain) as string 
set new_folder to (pic_folder & "new") as file specification

It would be nice if something like this even worked

tell application "System Events"
	set theDesktops to a reference to (every desktop whose display name is "Cinema HD")
	set picture of item 2 of theDesktops to file "Desktop 5B.jpg" of folder "SwitchResX" of pdoc from user domain
end tell

I hope that wasn’t too confusing.

For your particular script, any reason the more boring Finder forms won’t work? Here’s a bunch as examples:

tell application "Finder"
	set testPath1 to path to movies folder
	set testPath2 to path to pictures folder
	set testPath3 to path to desktop folder
	set testPath4 to path to temporary items folder
end tell

Those paths do not require the Finder:

set testPath1 to path to movies folder
set testPath2 to path to pictures folder
set testPath3 to path to desktop folder
set testPath4 to path to temporary items folder

Will work as well.

Hope it helps,
ief2

If you need System Events anyway, you can use System Events’ special folder references.
pictures folder is the reference to the pictures folder of the current user

tell application "System Events"
	set theDesktops to a reference to (every desktop whose display name is "Cinema HD")
	set picture of item 2 of theDesktops to file "Desktop 5B.jpg" of folder "SwitchResX" of pictures folder
end tell

Hello.

If you google a little bit you should find something far more elaborate than this.
This is somebody else’s work than mine, and I really can’t remember where I got it from. I guess this is within line
of what you wanted in the first place. -But the usefulness of this . The only context this is useful in is within the script below where it saves some lines of code. In general you only use a couple of such folders and the your code is much more readable than path to “cusr” where path to home would be a little bit easier to grasp
after a year. -Like the other guys I se no other use than confuse with this. It is much better to the folder names spelled out fully, or relative to a home folder. This is just meant as a friendly advice without any pointing finger. :slight_smile:


set the folders_list to {{"Applications", "apps"}, {"Audio Support", "adio"}, {"Documentation", "info"}, {"Documents", "docs"}, {"Favorites", "favs"}, {"Home", "cusr"}, {"Library (user)", "dlib"}, {"Movies", "mdoc"}, {"Music", "µdoc"}, {"Pictures", "pdoc"}, {"Preferences (user)", preferences folder}, {"Public", "pubb"}, {"Scripts", scripts folder}, {"Scripting Additions", scripting additions folder}, {"Shared", "sdat"}, {"Sites", "site"}, {"Users", "usrs"}, {"Utilities", "utiÆ’"}}

set the folder_names to {}
set the folder_codes to {}
repeat with i from 1 to the count of the folders_list
	copy item i of the folders_list to {this_name, this_code}
	set the end of the folder_names to this_name
	set the end of the folder_codes to this_code
end repeat

set the user_domain to {"Library (user)", "Scripts"}
set the system_domain to {"Applications"}

activate

set the chosen_folder to choose from list folder_names with prompt "Choose folder to open:"
if the chosen_folder is false then return "user cancelled"
set the chosen_folder to the chosen_folder as string
repeat with i from 1 to the count of the folder_names
	set this_name to item i of the folder_names
	if this_name is the chosen_folder then
		set the folder_code to item i of the folder_codes
		exit repeat
	end if
end repeat

tell application "Finder"
	activate
	try
		if the chosen_folder is in the user_domain then
			open (path to the folder_code from user domain)
		else if the chosen_folder is in the system_domain then
			open (path to the folder_code from system domain)
		else
			open (path to the folder_code)
		end if
	on error error_message
		beep
		display dialog error_message buttons {"OK"} default button 1
	end try
end tell

Best Regards and good luck

McUsr

Wow thank you so much for all the posts this is wonderful and I feel the love! StefanK thanks for posting that implemented into my code. That is what I got from CalvinFold and ief2 as well but for some reason it is not working for me.

For some reason this is not working for me even though I do have a picture in this path. I even tried adding “of home folder” at the end which didn’t seem like it would work since it was redundant and it sure enough it didn’t work.

tell application "System Events"
	set theDesktops to a reference to (every desktop whose display name is "Cinema HD")
	set picture of item 2 of theDesktops to file "Desktop 5B.jpg" of folder "SwitchResX" of pictures folder of home folder
end tell

I also tried moving the picture directly in the pictures folder to see if it would work then but it does the same thing as before of changing the desktop to the default Mac Desktop of "Aurora.jpg found in Mac HD:Library:Desktop Pictures:Nature:Aurora.jpg. I am not sure if that is where it is pulling it from but that is the picture it changes it to and “Desktop 5B.jpg” is not that picture.

tell application "System Events"
	set theDesktops to a reference to (every desktop whose display name is "Cinema HD")
	set picture of item 2 of theDesktops to file "Desktop 5A.jpg" of pictures folder
end tell

The problem is the reference to the desktop object, not the picture.

Try this


tell application "System Events"
	set desktop1 to (a reference to (1st desktop whose display name is "Cinema HD"))
	set desktop2 to (a reference to (2nd desktop whose display name is "Cinema HD"))
	set picture of desktop1 to (file "Desktop 5A.jpg" of folder "SwitchResX" of pictures folder) as alias
	set picture of desktop2 to (file "Desktop 5B.jpg" of folder "SwitchResX" of pictures folder) as alias
end tell

Works like a charm thank you very much, that is perfect!

McUsr, The applescript you posted is actually Apple Sample Code. It is from the script “Open Special Folder” from the “Navigation Scripts” subset of the standard system scripts which ship with OSX. :slight_smile: