Difficulty with script to use a temp file OS9 and OS X

Hi…

I’m working on a script that needs to read and write to a temp file. I’m hoping to make the same script run on OS X as well as pre-X. I don’t need (or want) the temp file to be invisible. My question is where to ‘store’ the temp file on X.

On a pre-X system, I might put it in the Preferences folder. But my ignorance about X (and the notion of multiple users, permissions and so on) is so complete that I don’t know whether the user’s (HD:Users:username:Library:Preferences:) preferences folder (or any of the other ‘special’ folders, for that matter) is always accessible for this type of use.

Put another way, I’d like to be able to say:

set temp_file to ((path to preferences folder) as text) & “Temp

Is this an acceptable place to put the temp file where it can always be accessed by the script? I could use one of the other special folders that can be called by ‘path to’, but the folder must be referenced the same way on X and pre-X.

If I’m not making much sense, it should be no surprise, but if someone gets the drift of what I’m trying to do, I’d really appreciate the help.

Thanks…

(And thanks to the Macscripter crew for this resource !)

Peter B.


I think this should work on both 9 & X systems:

set temp_file to ((path to temporary items) as text) & "_Temp_"

Although the path to which it resolves will be different depending on the system. The only caveat to this (which may be a benefit) is that these files are flushed automatically by the system on restart/logouts. If you want your file to be more persistent, then the Preferences folder is OK but you’re probably better off with the Application Support Folder.

I use this script based on Rob’s terrific work to help me when I’m working with paths. You can save it as a script and add it to the Script Menu:

property routine_name : "set path to special folder"
-- Based on "Show Path Command" Version 1.0.2 by Rob Jorgensen
property folder_codes : {"- System -", "root", "macs", "dtop", "rotf", "", "- System Library -", "dlib", "?lib", "sync", "prof", "cscr", "cmpd", "csrv", "kext", "fsys", "shar", "fnds", "font", "fram", "klay", "?mod", "sprf", "impr", "ppdf", "pfrm", "qtex", "wcmp", "?scr", "ssnd", "spch", "fvoc", "", "- Computer -", "apps", "aex?", "uti?", "ddoc", "devh", "utmp", "temp", "flnt", "usrs", "sdat", "", "- Computer Library -", "asup", "adio", "midi", "aplg", "acmp", "asnd", "alrt", "bank", "cach", "cmnu", "dtp?", "dsrv", "dplg", "info", "?hlp", "auth", "indx", "rcpt", "ilgf", "empz", "", "- User -", "cusr", "desk", "sdsk", "trsh", "empt", "docs", "favs", "fbcx", "issf", "issd", "kchn", "mang", "pref", "rapp", "rdoc", "rsvr", "scr?", "fasf", "mdoc", "?doc", "pdoc", "pubb", "site"}
property item_delim : "-----------------------------------------------------------"
property folder_headers : {"All Special Folders", item_delim}

copy {folder_headers, {"false", item_delim}, ""} to {the_folders, invalid_choices, return_string}
set the_codes to folder_headers & folder_codes
set code_count to (count of the_codes)

repeat with i from 3 to code_count
	set the_code to (item i of the_codes)
	try
		copy (the_code & " =  " & tab & ((path to the_code as string) & return)) to end of the_folders
	on error
		copy the_code & return to end of the_folders
		if the_code is not "All Special Folders" then
			if the_code is not in invalid_choices then copy the_code & return to end of invalid_choices
		end if
	end try
end repeat

tell application "Script Editor"
	activate
	repeat
		set chosen_code to (choose from list the_folders with prompt "Choose Special Folder:" cancel button name "Cancel" OK button name "Copy to Clipboard" with empty selection allowed without multiple selections allowed) as string
		if chosen_code is in invalid_choices then
			beep
			display dialog "You made an invalid selection. Please try again." buttons {"OK"} default button 1 with icon 2
		else
			exit repeat
		end if
	end repeat
	if chosen_code = "All Special Folders" then
		set the_codes to folder_codes
	else
		set the_codes to {(text 1 thru 4 of chosen_code)}
	end if
	set the_button to button returned of (display dialog ("Set the path(s) to properties?") buttons {"No", "Yes"} default button 2 with icon 1)
end tell

set code_count to (count of the_codes)
repeat with i from 1 to code_count
	set the_code to (item i of the_codes)
	try
		set the_path to (path to the_code) as string
		my atid(":")
		set folder_name to text item -2 of the_path
		my atid("")
		set folder_name to snr(folder_name, " ", "_")
		
		if the_button = "Yes" then
			set return_string to return_string & ("property path_to_" & folder_name & " : (path to " & the_code & ") as string") & return
		else
			set return_string to return_string & ("set path_to_" & folder_name & " to (path to " & the_code & ") as string") & return
		end if
	end try
end repeat

set the clipboard to return_string

tell application "Script Editor"
	activate
	display dialog ("The path to ?" & chosen_code & "? has been placed on clipboard. You may now paste it into a script window.") buttons {"?"} default button 1 with icon 1 giving up after 5
end tell
return return_string

on atid(the_delim)
	set AppleScript's text item delimiters to the_delim
end atid

on snr(the_string, search_string, replace_string)
	my atid(search_string)
	set the_string to (every text item of the_string) as list
	my atid(replace_string)
	set the_string to (every text item of the_string) as string
	my atid("")
	return the_string
end snr

Jon

Jon:

Thanks for your reply…

I will probably opt for the Preferences or Application Support folders (if the latter exists as an option in pre-X), because I want the user to be able to view (or trash) the temp file readily.

‘Flushing’ may be a desirable option in future, and I only discovered the temporary items folder on 8.6 today.

(It’s a great big world out there, ain’t it?)

Thanks to both you and Rob…

Peter B.