简体   繁体   中英

AWK, Applescript, String/Int Parsing

This is a bit in depth, and I'm in a bit over my head. I've never used AWK, and the more I try to learn about it (perhaps it's the lateness of the hour) the more frustrating it's getting to be.

I'm trying to write an applescript... script to resize windows across my monitors. Now, I COULD take the easy route and hardcode my monitor dimensions into the script, but instead I decided to try and gather them dynamically. I've managed to find a code snippet that parses a plist and spits out ALMOST-useable info, here's the applescript:

set screenInfo to do shell script "defaults read /Library/Preferences/com.apple.windowserver | awk '
        BEGIN { FS=\"= \" }
        /Active/  { screens++ }
        { gsub(\";\",\"\",$2) }
        /^ *OriginX/ { ox[screens] = $2 }
        /^ *OriginY/ { oy[screens] = $2 }
        /^ *Width/   { w[screens]  = $2 }
        /^ *Height/  { h[screens]  = $2 }
        END       {
                for (si=1;si<=screens;si++) {
                    print ox[si],oy[si],w[si],h[si]
                    }
            }'"
    return screenInfo
end getScreenInfo

That's working pretty well - in my "result" window, I get the following:

"0 0 1920 1080  
\"-1600\" 0 1600 1200  
1920 0 1680 1050  
0 0 1920 1080  
\"-1600\" 0 1600 1200  
1920 0 1680 1050  
3600 1050 1024 768  "

The first number pair is the coordinates for the top left corner of the monitor, the second number pair is the resolution of that monitor. Such is the case for each monitor - though there are duplicates of monitors 1-3, plus a ghost monitor. I don't really care about that, it should be parse-able.

My problem comes when I try to parse those numbers as ints so I can use them - using something like set theVar to word 1 of paragraph 2 of theString as integer , it parses as 1600 , not the -1600 it needs to be. I tried stepping through the entire string, 1 character at a time, to remove the escaping backslashes, but I think those only actually show up in the results window - when I display dialog , they don't show. Even when I don't try to parse it as an integer, just word 1 of paragraph 2 it ignores the - and returns "1600" . How else can I parse this? Goodness, I'm not a fan of applescript right now.

You can get the screen resolutions with AppleScriptObjC

use framework "Foundation"

set screenFrames to {}
set screens to current application's NSScreen's screens()
repeat with aScreen in screens
    set end of screenFrames to aScreen's frame() as record
end repeat

screenFrames will contain a record for each screen containing records origin with x and y members and size with width and height members.

For example you can get the width of screen 1 with

set screen1Width to width of |size| of item 1 of screenFrames as integer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM