Send AppleScript Result to TextEdit or Clipboard

Hi,

I would like to ask of your help to send the result from this simple script to TextEdit or the ClipBoard.


tell application “Finder”
get properties of windows
end tell

Thanks you very much for any assistance!
Franco

The problem with getting properties is you have to find a way to make the class info in to text.

Here I force an error that will put the Property info into the error dialogue as text.

tell application "Finder"
	try
		set winProps to (get properties of windows)
		get name of winProps
	on error err
		set oldDelims to AppleScript's text item delimiters -- get normal dilm
		set AppleScript's text item delimiters to {"Can't get name of "}
		set Win_props to text item 2 of err
		set AppleScript's text item delimiters to oldDelims
		set the clipboard to Win_props
		tell application "TextEdit"
			activate
			make document with properties {text:Win_props}
			
		end tell
	end try
end tell

As error messages are localized, this script works only when the system is set to English.

At this time I’m searching which System file contains the used localized resources.

This edited version is usable worldwide:


tell application "Finder"
	try
		set winProps to (get properties of windows)
		get name of winProps
	on error err
		
		set oldDelims to AppleScript's text item delimiters -- get normal dilm
		set AppleScript's text item delimiters to {"name of "}
		set Win_props to text item 2 of err
		set AppleScript's text item delimiters to oldDelims
		set the clipboard to Win_props
		tell application "TextEdit"
			activate
			make document with properties {text:Win_props}
		end tell
	end try
end tell

Yvan KOENIG (from FRANCE vendredi 6 février 2009 10:05:42)

Thank you both very much for your replies!

I have figured out since my first post how to get just the information I needed with the script below. Could you please help me incorporate this into the same thing that you had done for me originally?

Thanks-
Franco

tell application “Finder”
set currwindow to front window
get bounds of currwindow
end tell

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
	
tell application "TextEdit"
		activate
		make new document with properties {text:tm}
	end tell
end tell

Amazing community! Thank you so much everyone for your assistance. I have only been playing with Apple Script for about a week now, but thanks to your help I am very excited about continuing on with my education of scripting. I would like to share with you the project that spawned this.

http://www.macupdate.com/info.php/id/30277

Thanks again, Frank

This isn’t now relevant to francola’s revised requirements, but a more obviously robust delimiter to use for a complete list of property records would be the opening brace of the list itself. Also the closing brace ” to lose anything that comes after it in the error message, such as the full stop:

try
	tell application "Finder" to set winProps to (properties of windows)
	get || of winProps
on error err
	set oldDelims to AppleScript's text item delimiters -- get normal dilm
	set AppleScript's text item delimiters to "{"
	set Win_props to "{" & text from text item 2 to -1 of err
	set AppleScript's text item delimiters to "}"
	set Win_props to text 1 thru text item -2 of Win_props & "}"
	set AppleScript's text item delimiters to oldDelims
	
	-- set the clipboard to Win_props -- Or:
	tell application "TextEdit"
		activate
		make new document at front with properties {text:Win_props}
	end tell
end try

Thanks Nigel

Of course it’s the cleaner soluce.

But, if you know where are stored the localized strings used for the error messages, I am interested :wink:

Yvan KOENIG (from FRANCE samedi 7 février 2009 14:08:22)

I guess here, Yvan:
/System/Library/Components/AppleScript.component/Contents/Resources/

Thank you for all of the help! I would like to ask if someone could please help me integrate the two scripts included in the attached download into one. I would like to have the action of this first script (attaining the window bounds) parsed into the second script which at his point I am having to open seperatly and then paste into. Thanks!

Actually made into one would not really be the desired effect. I really need to get the window bounds properties over into the second script or maybe somehow have the first script build it into itself and then reconstruct itself so that it may be used as the button. Sorry if thats a mess, but I am having trouble wrapping my brain around this. :slight_smile:

DOWNLOAD

maybe:


property myBounds : {854, 391, 1260, 697}

set applyButton to "Apply"
set resetButton to "Reset"
set cancelButton to "Cancel"
set quoi to display dialog "Choose an action." buttons {cancelButton, resetButton, applyButton} default button applyButton
try
	set quoi to button returned of quoi
on error
	error number -128
end try
if quoi is cancelButton then error number -128

if quoi is resetButton then
	tell application "Finder" to tell front window
		set myBounds to get bounds
	end tell -- Finder
else
	tell application "Finder" to tell front window
		set the bounds to myBounds
		set the current view to icon view
		set the sidebar width to {0}
		set {wLeft, wTop, wRight, wBottom} to get bounds -- ?? why ??
	end tell -- Finder
end if

Yvan KOENIG (from FRANCE samedi 7 février 2009 21:41:59)

Very nice work. Thank you!

I would really like to keep the final toolbar button in a one click mode to return to the desired window state. Would it be possible with AppleScript to get the window bounds and then generate another script from within that script that would create the final button script with the desired window bounds in place. Or is it possible to write a script that could look at the clipboard to see the series of window bounds numbers and parse them into place so that the user would not have to open the script and physically paste? Another thought I had is maybe the first script generates a physical text file (maybe a hidden file) that the final button script looks at whenever it is pressed. Thanks for all of your help!

Frank

hey francola,

odd coincidence that I found this, I have been trying to make this a little more elegant as well.
here is the latest post for me in the macthemes thread:

http://macthemes2.net/forum/viewtopic.php?pid=416801#p416801

Something I am very interested in, is creating a script that will:

Get bounds info of all open windows of all applications

Then I would like to create another script that would set the bounds of those particular applications to the earlier result.

basically, a script to run to set the position of your windows.

and another one to reset the positions and size to that stored value.

Maybe this could be done by having the first script write a text file (preference file) with the window bound info, then have the second usage script use that stored info?

if this is possible all help would be appreciated.

here is the script I am currently using as a solution to organize my commonly used non full-screen windows. Works both on the single display of my MBP or when connected to my Cinema Display to move itunes over to the extended desktop.

--GET SCREEN INFORMATION
tell application "Finder"
	set currwindow to front window
	set screen_resolution to bounds of window of desktop
	set screen_width to item 3 of screen_resolution
	set screen_height to item 4 of screen_resolution
	set the sidebar width of currwindow to {0}
end tell

--SET DESIRED WIDTH OF BASIC WINDOWS (DUAL DISPLAY)
set desired_width to 1440

--SET BASIC SCREEN POSITION (Left, Right, Top, Bottom)
set left_bound to 22
if screen_width > 1920 then
	set right_bound to left_bound + desired_width
else
	set right_bound to screen_width - 220
end if
set top_bound to 46 (* for the menu bar *)
set bottom_bound to screen_height - 22

--SET ITUNES DESIRED WIDTH (DUAL DISPLAY)
set iTunesdesired_width to 1600

--SET ITUNES SCREEN POSITION (Left, Right, Top, Bottom)
set iTunesleft_bound to 1947
set iTunesright_bound to screen_width - 22
set iTunesbottom_bound to screen_height - 22
set iTunestop_bound to -100

--POSITION APPLICATIONS
try
	tell application "Firefox"
		activate
		set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound}
	end tell
end try

try
	tell application "iTunes"
		activate
		if screen_width > 1920 then
			set the bounds of the first window to {iTunesleft_bound, iTunestop_bound, iTunesright_bound, iTunesbottom_bound}
		else
			set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound}
		end if
	end tell
end try

--SIZE AND POSITION FINDER WINDOWS
tell application "Finder"
	activate
	set bounds of Finder window 1 to {675, 83, 1700, 609}
	set bounds of Finder window 2 to {675, 647, 1700, 1172}
end tell

(looking at this, it could be simplified by just using the bound info generated by these other scripts, there MUST be a way to take all the info returned from the below scripts, and save them in a text or .plist file that the usage script can reference for the sizes right?

and the scripts to return the bounds info:

For Finder:

tell application "Finder"
	get bounds of windows
end tell

For Firefox (or other apps):

tell application "Firefox"
	get bounds of first window
end tell

Model: MacBook Pro 17" 2.4
Browser: Firefox 3.0.6 GTB5 Ubiquity/0.1.5
Operating System: Mac OS X (10.5)

alright, I know have come up with a script that spits out text files for a few different apps.

set AppleScript's text item delimiters to ","
tell application "Finder"
	get bounds of Finder window 1 & bounds of Finder window 2
	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
	tell application "TextEdit"
		activate
		make new document with properties {text:tm}
	end tell
end tell

tell application "Firefox"
	get bounds of first window
	set theresult to the result
	
	set {r, s, t, q} to the result
	set tm to the result
	set tm to tm as text
	
	set the clipboard to tm
	
	tell application "TextEdit"
		activate
		make new document with properties {text:tm}
	end tell
end tell

tell application "iTunes"
	get bounds of first window
	set theresult to the result
	
	set {r, s, t, q} to the result
	set tm to the result
	set tm to tm as text
	
	set the clipboard to tm
	
	tell application "TextEdit"
		activate
		make new document with properties {text:tm}
	end tell
end tell

tell application "Google Reader"
	get bounds of first window
	set theresult to the result
	
	set {r, s, t, q} to the result
	set tm to the result
	set tm to tm as text
	
	set the clipboard to tm
	
	tell application "TextEdit"
		activate
		make new document with properties {text:tm}
	end tell
end tell

tell application "iCal"
	get bounds of first window
	set theresult to the result
	
	set {r, s, t, q} to the result
	set tm to the result
	set tm to tm as text
	
	set the clipboard to tm
	
	tell application "TextEdit"
		activate
		make new document with properties {text:tm}
	end tell
end tell

there must be a way to simplify this or have it all in one text document

like this:

Finder: “window bound data”

Firefox: “window bound data”

iTunes: “window bound data”

etc.

is it possible to add more than just the “{text:tm}” property to one document?

I think I have now found a very elegant solution for multiple Finder windows and multiple apps, using methods used by franco and others:

HERE IS A .zip of my multi window release, I will post it on the MacThemes2 thread as well.
http://www.andrewandoru.com/downloads/labs/layout.zip

thanks for the help and info guys!