Mac Applescript Error - Can't set window to...

tbmx

macrumors newbie
Original poster
Sep 17, 2006
28
0
Hello,

I'm new at applescript and trying to make a file launcher that I plan on eventually expanding, only problem is I can't get it to fill with the proper path! I know in the example below I could probably more easily tell the application to open the file but I plan on using this down the road with a few programs where I don't believe that will work.

Code:
set myFiles to {"File1", "File2", "File3", "File4"}

choose from list myFiles with title "Select File"

if result = {"File3"} then
	set filepath to "/harmony.xml"
	application "TextEdit" activate
	tell application "System Events"
		keystroke "o" using {command down}
		keystroke "/"
		delay 0.5
		tell window "Open"
			tell sheet 1
				set text field 1 to filepath
			end tell
			click button 1
			delay 0.5
		end tell
	end tell
else
	application "TextEdit" activate
end if
Each time I run this I get the following error:

System Events got an error: Can’t set window "Open" to "/harmony.xml".

I have tried a few different programs and file types to no avail. I'm running 10.6.2. Any help would be appreciated.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,784
143
Harrogate
Why are you sending keystrokes etc?

This will open file "/Harmony.xml" in TextEdit:

Code:
tell application "TextEdit"
	open file "/Harmony.xml"
end tell
 
Comment

chown33

Moderator
Staff member
Aug 9, 2009
8,671
4,942
vertical
I know in the example below I could probably more easily tell the application to open the file but I plan on using this down the road with a few programs where I don't believe that will work.
What are the programs where you don't think the 'open' scripting command will work? Be specific.

If those programs are even barely scriptable, they should respond to the 'open' command, because that's a basic required one. If they aren't scriptable, then they won't be scriptable at all, except possibly by UI Scripting. UI Scripting requires a different strategy, though.

http://www.macosxautomation.com/applescript/uiscripting/index.html
http://prefabsoftware.com/uibrowser/

Maybe you should explore the option of using the 'keystroke' command to iterate every character in your pathname. That is, precisely simulate the action of entering it by keystrokes.

You might also look at Watch Me Do in Automator:
http://www.apple.com/macosx/what-is-macosx/apps-and-utilities.html#automator
 
Comment

tbmx

macrumors newbie
Original poster
Sep 17, 2006
28
0
Thank you both for the quick response.

The initial program I'm trying to get worked out is Kega Fusion (32x, SegaCD emu). Unfortunately, it does not have a straightforward open command as it has one listed for each file type hence sending keystrokes. I would use the keyboard and mouse but am going to be executing this script from Plex Media Center via a harmony remote. Ideally all I would need is the remote to execute the scripts and launch files with various programs. This will ultimately just be one piece of a much larger script that interacts with multiple programs.

My initial strategy was to use the method robbieduncan suggested but it will open and quit unexpectedly. I also contemplated sending the entire file path via individual keystrokes but was trying to avoid having to do so.

I've done some gui scripting before but can't think of a way to have a dynamic filepath based on a defined list.

Once again, I appreciate the insight/help.
 
Comment

mysterytramp

macrumors 65816
Jul 17, 2008
1,335
3
Maryland
If the app doesn't take the "open" message you should be able to say:

tell application "Finder"

open filename.ext using "name of another app"

end tell

mt
 
Comment

chown33

Moderator
Staff member
Aug 9, 2009
8,671
4,942
vertical
Code:
	set filepath to "/harmony.xml"
	application "TextEdit" activate
	tell application "System Events"
		keystroke "o" using {command down}
		keystroke "/"
		delay 0.5
		tell window "Open"
			tell sheet 1
				set text field 1 to filepath
			end tell
			click button 1
			delay 0.5
		end tell
	end tell
I did some trials and the above code isn't using the proper constructs. The "System Events" app doesn't have a window "Open", so the tell window "Open" will always fail.

What "System Events" has is a bunch of application processes. You must name the target process and the specific UI element, then it will work. Example:

Code:
set filepath to "/usr/include/"
application "TextEdit" activate
tell application "System Events"
	keystroke "o" using {command down}
	keystroke "/"
	delay 0.5
	keystroke "a" using {command down}
	keystroke filepath
	delay 0.5
	-- get UI elements of sheet 1 of window "Open" of application process "TextEdit"
	click button "Go" of sheet 1 of window "Open" of application process "TextEdit"
	delay 0.5
end tell
The commented-out get UI Elements ... will return a list of the available UI elements at that point.


To begin, I opened the Scripting Dictionary of System Events.app and read the info for keystroke. It clearly shows a string, and the info says that every character in the string is entered as if by actual keystrokes.

Then I started with this script:
Code:
set filepath to "/usr/include/"
application "TextEdit" activate
tell application "System Events"
	keystroke "o" using {command down}
	keystroke "/"
	delay 0.5
	keystroke "a" using {command down}
	keystroke filepath
	get UI elements
end tell
The resulting list of application processes showed me the first identifier to put after get UI elements, yielding this second script:
Code:
set filepath to "/usr/include/"
application "TextEdit" activate
tell application "System Events"
	keystroke "o" using {command down}
	keystroke "/"
	delay 0.5
	keystroke "a" using {command down}
	keystroke filepath
	get UI elements of application process "TextEdit"
end tell
Then it was just an iterative process of looking at the listed UI elements and modifying the get UI elements identifier.

A lot of success in programming comes mainly from RTFM and doing obvious stuff.
 
Comment

Similar threads

Register on MacRumors! This sidebar will go away, and you'll see fewer ads.