A practical way to store script values

Hello

This is just a little practical method of storing values for your script I have put in two paths there, which both serves different purposes, given the nature of your script, if the values are to be sustained, but can be reproduced, then library caches, is the obvious place to save your scripts properties. If it is a one off example, like it really is for the given example. Then the temporary items folder is the obvious place.

I fiddled with this, really not having a standardized place to store script values, and finally saw Daniel Jalkut use this, and I found this to be a great idea!

The reason for storing script values, is for those scripts not being run by a script runner that saves values, like quick silver for instance.

The script I show you, is not made by me, it is made by jon8 but I have added the code, to make it persistent when running Quicksilver, and as such must surely be edited, as the values within for the screen resolution are mine, and hardcoded!

The point here is the pattern for retrieving and storing values, and not the script


global scriptpath
global cachespath
-- the script as such is copyright jonn8
-- I have used used it to illustrate the way to store and retrieve values painlessly
-- having no better example to whip up at the moment
set scriptpath to (path to temporary items folder from user domain as text) & "net.mcusr.coverflowvals"
set cachespath to (path to library folder from user domain as text) & "caches:" & "net.mcusr.coverflowvals"
try
	set flow_window to load script alias scriptpath
on error
	script coverflowVals
		property original_bounds : missing value
		property original_view : missing value
	end script
	set flow_window to coverflowVals
end try

if my flow_window's original_bounds = missing value then
	my expand_window()
else
	my revert_window()
end if
store script flow_window in scriptpath replacing yes

on expand_window()
	tell application "Finder"
		activate
		try
			if class of window 1 is not Finder window then return
			tell Finder window 1
				set my flow_window's original_bounds to (get bounds)
				set my flow_window's original_view to (get current view)
				
				set bounds to {0, 44, 1680, 1050}
				set current view to flow view
			end tell
		end try
	end tell
end expand_window

on revert_window()
	tell application "Finder"
		activate
		try
			if class of window 1 is Finder window then
				tell Finder window 1
					set bounds to my flow_window's original_bounds
					set current view to my flow_window's original_view
				end tell
			end if
		end try
	end tell
	set my flow_window's original_bounds to missing value
	set my flow_window's original_view to missing value
end revert_window

Hello.

The script as such was just an illustration of a technique, rereading this post made me realize that the improvement of the script had gone into the far-away-archieve.

It is not much of an improvement, but the script is now hamstrunged for trying to revert windows it never expanded, and will expanded windows, even if the last operation on another window was to expand.

I don’t keep a history of Finder Window’s and their state, as that is too much for something simple.

global scriptpath
global cachespath

set scriptpath to (path to temporary items folder from user domain as text) & "net.mcusr.coverflowvals"
set cachespath to (path to library folder from user domain as text) & "caches:" & "net.mcusr.coverflowvals"
try
	set flow_window to load script alias scriptpath
on error
	script coverflowVals
		property original_bounds : missing value
		property original_view : missing value
		property fwin_id : missing value
	end script
	set flow_window to coverflowVals
end try

if my flow_window's original_bounds = missing value then
	my expand_window()
else
	my revert_window()
end if
store script flow_window in scriptpath replacing yes

on expand_window()
	tell application id "MACS"
		activate
		try
			if class of window 1 is not Finder window then return
			tell Finder window 1
				set my flow_window's original_bounds to (get bounds)
				set my flow_window's original_view to (get current view)
				set my flow_window's fwin_id to id of it
				set bounds to {0, 44, 1680, 1050}
				set current view to flow view
			end tell
		end try
	end tell
end expand_window

on revert_window()
	local doneIt
	set doneIt to false
	tell application id "MACS"
		activate
		try
			if class of window 1 is Finder window then
				tell Finder window 1
					if id of it = my flow_window's fwin_id then
						set bounds to my flow_window's original_bounds
						set current view to my flow_window's original_view
						set doneIt to true
					end if
				end tell
			end if
		end try
	end tell
	if doneIt then
		set my flow_window's original_bounds to missing value
		set my flow_window's original_view to missing value
	else
		expand_window()
	end if
end revert_window