Frage

I'm trying to learn AppleScript. What you see below a part of my 1st ambitious project. It's modified so that it can be test within the AppleScript editor if you also have an TextEdit window open.

What the script does:

  • choose editor from list
  • align the two opened windows

My problem:

Aligning the windows works only if I dismiss the variable. As soon as I replace the variable returned from the list (selectedEditor) with a string tell process "TextEdit" it works.

I hope someone could spot the error.

Error code from the events log:

System Events got an error: Can’t make {"TextEdit"} into type integer.

Here's the code:

property myEditors : {"TextEdit", "Sublime Text 2"}
set the editorList to myEditors as list

set selectedEditor to choose from list the editorList

set lngWidth to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width")
set lngHeight to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height")
set lngHalf to lngWidth / 2
set lngHeight to lngHeight - 22

tell application id "sevs"
    tell process selectedEditor to tell window 1 to set {position, size} to {{lngHalf, 22}, {lngHalf, lngHeight}}
    tell process "AppleScript Editor" to tell window 1 to set {position, size} to {{0, 22}, {lngHalf, lngHeight}}
end tell
War es hilfreich?

Lösung

The error "System Events got an error: Can’t make {"TextEdit"} into type integer." is telling you the problem. {"TextEdit"} is a list with one item. That's what you get back from the "choose from list" statement. Therefore change that statement to this...

set selectedEditor to item 1 of (choose from list the editorList)

That will give you "TextEdit", which is a string, as opposed to {"TextEdit"} which is a list.

Also, this statement is unnecessary because myEditors is already a list as evidenced by the brackets around it. Just use myEditors directly in the "choose from list" command.

set the editorList to myEditors as list

Andere Tipps

The only clue I see for this error is that it is:

"errAEIllegalIndex: index is out of range in a put operation"

I don't have/own Marked, so I don't know its limitations/potential with AppleScript. Since you're new to AppleScript, I'll ask you if you're sure Marked.app is not scriptable, that is, if you're sure it does not have an appropriate scripting dictionary (at all). Dropping the app file onto AppleScript Script editor would tell you this (either it shows you the app's dictionary, or tells you it is unable to read it). The only reason to use System Events is if the app is not scriptable, or is scriptable in a very limited way. Most apps with even limited scriptability have window objects with properties.

For example, Firefox (which I'm using right now) has limited scriptability and allows me to set the bounds of its windows:

tell application "Firefox"
    set bounds of window 1 to {137, 22, 1345, 809}

    properties of window 1
end tell

... and also get its windows' properties (the 2nd line in the tell).

I'm sorry if this is obvious to you and you've already determined that Marked has none of this functionality, but it's the first thing to check, and like I said, I don't have Marked.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top