Search code examples
variableswindowalignmentapplescriptsystemevent

Can't align windows when using a variable → Can't make {\"string\"} into type integer


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

Solution

  • 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