Applescript to "center" in Bricksmith


Applescript to "center" in Bricksmith
#1
Aloha,

Below is an Applescript to "center" parts in Bricksmith. It computes the averages, denoted x, y, and z, of the coordinates of the selected parts and then shifts -x, -y and -z (and snaps to grid).

It requires UI Scripting to be on (and does not check if it is).

Enjoy!

Ramón

Code:
property wait : 2 -- seconds to wait for Bricksmith to react

set the clipboard to "0"
activate application "Bricksmith"
tell application "System Events"
    tell process "Bricksmith"
        -- Copy
        keystroke "c" using {command down}
    end tell
end tell

-- Wait for the clipboard to update "wait" seconds
repeat wait * 10 times
    if (the clipboard) is "0" then
        delay 0.1
    else
        exit repeat
    end if
end repeat

if (the clipboard) is not "0" then
    set parts to the clipboard as text
    try
        set oldDelims to AppleScript's text item delimiters -- save their current state
        set AppleScript's text item delimiters to {" "} -- declare new delimiters
        
        set n to 0
        set x to 0
        set y to 0
        set z to 0
        
        repeat with part in paragraphs of parts
            try
                if text item 1 of part is "1" then
                    set n to n + 1
                    set x to x + (text item 3 of part) as real
                    set y to y + (text item 4 of part) as real
                    set z to z + (text item 5 of part) as real
                end if
            end try
        end repeat
        
        if n > 0 then
            -- Compute averages
            set x to round (x / n)
            set y to round (y / n)
            set z to round (z / n)
            
            activate application "Bricksmith"
            tell application "System Events"
                tell process "Bricksmith"
                    
                    -- Open the "Move" window
                    click menu item "Move…" of menu 1 of menu bar item "Edit" of menu bar 1
                    
                    -- Wait for the move window to open "wait" seconds
                    set flag to false
                    repeat wait * 10 times
                        try
                            get focused of window "Move"
                            set flag to true
                            exit repeat
                        end try
                        delay 0.1
                    end repeat
                    if not flag then
                        return
                    end if
                    
                    -- Type the new x coordinate
                    if (value of text field 3 of window "Move") is not (-x as string) then
                        set focused of text field 3 of window "Move" to true
                        repeat with c in characters of (-x as string)
                            keystroke c
                        end repeat
                    end if
                    
                    -- Type the new y coordinate
                    if (value of text field 2 of window "Move") is not (-y as string) then
                        set focused of text field 2 of window "Move" to true
                        repeat with c in characters of (-y as string)
                            keystroke c
                        end repeat
                    end if
                    
                    -- Type the new z coordinate
                    if (value of text field 1 of window "Move") is not (-z as string) then
                        set focused of text field 1 of window "Move" to true
                        repeat with c in characters of (-z as string)
                            keystroke c
                        end repeat
                    end if
                    
                    -- Move
                    click button "Move" of window "Move"
                    
                    -- Close the move window
                    keystroke "w" using {command down}
                    
                    -- Snap to grid
                    keystroke "g" using {command down}
                    
                end tell
            end tell
        end if
        set AppleScript's text item delimiters to oldDelims -- restore them
    on error
        beep
        set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
    end try
else
    beep
end if
Reply
« Next Oldest | Next Newest »



Messages In This Thread
Applescript to "center" in Bricksmith - by Ramón Figueroa-Centeno - 2011-12-08, 23:59

Forum Jump:


Users browsing this thread: 1 Guest(s)