Save Results to Text File instead of Clip Board

I would like to be able to alter the following script to save to a text file (on the desktop for example) instead of the current clipboard. Thanks you very much for any help! Franco

set AppleScript's text item delimiters to ","
tell application "Finder"
	set currwindow to front window
	get bounds of currwindow
	set theresult to the result
	
	set {r, s, t, q} to theresult
	set tm to the result
	set tm to tm as text
	set the clipboard to tm
end tell

set AppleScript's text item delimiters to ","
tell application "Finder"
	set tm to (get bounds of front window) as text
end tell
set AppleScript's text item delimiters to ""

set fName to (do shell script "date " & quote & "+%Y%m%d-%H%M%S" & quote) & "." & "txt"

set p2d to path to desktop

tell application "Finder" to make new file at p2d with properties {name:fName}

write tm to file ((p2d as text) & fName)

Yvan KOENIG (from FRANCE lundi 9 février 2009 09:06:23)

Thank you for this! I would like to like to have this file named the same thing every time (“bounds” for example), replacing the old one upon creation. I have figured out how to name it specifically, but not how to make it replace itself. I would also like it to be in the Applications folder within a folder named FinderSize.

Thanks again! You are a scripting machine, and I am learning allot!

Hi.

I gather from your other thread that you want to use the saved text to set the bounds of other windows that you open. The other script will also set properties that only exist with a particular kind of ‘window’ called a ‘Finder window’ (the kind that shows folder or disk contents), so both scripts could be made a bit more robust by using this term specifically.

As from OS 10.5, all text handled within a script is UTF16 Unicode text, which is therefore what ‘write’ will write to the file unless told otherwise. The corresponding ‘read’ command, however, will assume that the file contains the old-style ‘string’ text unless told otherwise, so it’s best to use ‘as’ parameter in both cases. (These commands still differentiate between ‘Unicode text’ and ‘text’ or ‘string’ with regard to file contents.)

write tm as Unicode text to file filePath -- In this script.
set theBounds to (read file filePath as Unicode text) -- In the other.

-- Or:
write tm as string to file filePath -- In this script.
set theBounds to (read file filePath as string) -- In the other.

If the file’s only for the scripts’ use and you don’t particularly want to read it yourself, you could write the actual list to it, which would save having to manipulate text:

set dataFilePath to (path to desktop as Unicode text) & "Finder Window Bounds.dat" -- Substitute your path here.

tell application "Finder" to set FinderWindowOpen to ((count each Finder window) > 0)

if (FinderWindowOpen) then
	tell application "Finder" to set theBounds to bounds of front Finder window
	
	set fRef to (open for access file dataFilePath with write permission)
	try
		set eof fRef to 0
		write theBounds to fRef
	end try
	close access fRef
else
	-- Possibly display a message.
end if

The other script might then look something like this:

set dataFilePath to (path to desktop as Unicode text) & "Finder Window Bounds.dat" -- Substitute your path here.

tell application "Finder" to set FinderWindowOpen to ((count each Finder window) > 0)

if (FinderWindowOpen) then
	set theBounds to (read file dataFilePath as list)
	tell application "Finder"
		tell front Finder window
			set {bounds, current view, sidebar width} to {theBounds, icon view, 0}
			set {wLeft, wTop, wRight, wBottom} to bounds
		end tell
	end tell
else
	-- Possibly display a message.
end if

Amazing Nigel! Thank you for your help. Could you help me to understand setting a path? I would like this .dat file to reside in the Applications folder within a folder named FinderSize and even to be invisible if that is possible.

Hi Nigel,

I figured out how to set the path. I have been releasing this idea for free on MacUpdate and progressively getting it to work better. I would like to at the very least credit the MacScripter forums in general for your help. Let me know if you would like me to mention you specifically. Thanks again for all of your help. I have only been playing with scripts for a couple of weeks, but I am surely locked into it for life now. :slight_smile:

Hi, Franco.

Yes. The ‘path to’ command has several parameter keywords for folders (and other stuff) that it’ll find on anyone’s machine, regardless of the disk or user name. Among these is ‘applications folder’. By default, this is the Applications folder at the root level of the startup disk (‘local domain’), but you can also specify ‘from user domain’ if you want the current user’s personal Applications folder instead. ‘path to’ normally returns an alias, but (like ‘read’ and ‘write’) also has an optional ‘as’ parameter where you can specify you want text instead.

-- Defaulting to the root Applications folder:
set dataFilePath to (path to applications folder as text) & "FinderSize:Finder Window Bounds.dat"

-- Specifying the root Applications folder:
set dataFilePath to (path to applications folder from local domain as text) & "FinderSize:Finder Window Bounds.dat"

-- Specifying the user's Applications folder:
set dataFilePath to (path to applications folder from user domain as text) & "FinderSize:Finder Window Bounds.dat"

But a better place to keep your file (and the “FinderSize” folder) would be the user’s “Preferences” folder. This would be more in keeping with what Mac OS X’s designers had in mind and would allow each user to have his/her own bounds preference.

-- The default domain for 'preferences' is the user domain.
set dataFilePath to (path to preferences as text) & "FinderSize:Finder Window Bounds.dat"

It would be nice if the script that saved the bounds created the “FinderSize” folder too. You can get the Finder to do this, but it’s faster and less fuss to use the shell script command “mkdir”.

set PrefsPath to (path to preferences as text)
-- Create the "FinderSize" folder in the user's Preferences folder, if it doesn't already exist there.
do shell script ("mkdir -p " & quoted form of POSIX path of (PrefsPath & "FinderSize"))

set dataFilePath to PrefsPath & "FinderSize:Finder Window Bounds.dat"

tell application "Finder" to set FinderWindowOpen to ((count each Finder window) > 0)

if (FinderWindowOpen) then
	tell application "Finder" to set theBounds to bounds of front Finder window
	
	set fRef to (open for access file dataFilePath with write permission)
	try
		set eof fRef to 0
		write theBounds to fRef
	end try
	close access fRef
else
	-- Possibly display a message.
end if

On a slightly different matter arising from this thread:
In order to reduce bandwidth and to make threads easier to follow, would you mind not “top posting” here? Write your replies underneath what you’ve quoted and only quote as much as is necessary to show what you’re referring to. Very often, no quote is needed at all. Thanks! :slight_smile:

No problem Nigel! Thanks again for all of your help. This has been a great learning experience for me. I have attached a link to download the latest build of FinderSize if you are interested in seeing our work in a more polished state.

Franco
http://www.mediafire.com/file/3vuzdn223zn/FinderSize3.2.dmg