AppleScript, imposta la posizione della finestra in primo piano, anche se non AppleScriptable

0

Sto provando a scrivere uno script per centrare la finestra in primo piano dell'app attiva, quando seleziono questo AppleScript dal menu Script nella barra dei menu. Dovrebbe funzionare con qualsiasi app, comprese le app non scriptable.

Quando lo eseguo da Script Editor, funziona perfettamente. se seleziono l'app corrente o un'altra app dal menu dell'applicazione "tell" in Script Editor, si vede ancora che Script Editor è l'app in primo piano e sposta di conseguenza la finestra. (Ho anche eseguito il debug assegnando manualmente il nome di un'altra app a currentApplication , inclusa un'app non-scriptable, e funziona anche bene.)

Il problema sembra essere quando lo script viene eseguito dalla voce di menu Script. Ho eseguito il debug visualizzando una notifica con il testo di currentApplication e ogni volta che viene visualizzato il nome dell'applicazione in primo piano, come previsto. Tuttavia, non sposta la posizione della finestra. Cosa dà?

-- Get name of the current application and its front window
tell application "System Events"
    set activeApps to name of application processes whose frontmost is true
    set currentApplication to item 1 of activeApps
    -- DEBUG -- currentApplication seems to be the correct application
    -- display notification currentApplication
    -- activate currentApplication
    -- Get the front window and its measurements
    set frontWindow to the first window of application process currentApplication
    set windowSize to size of frontWindow
    set windowPosition to position of frontWindow
end tell

-- Get the bounds of the screen
tell application "Finder"
    set screenBounds to bounds of window of desktop
end tell

--calculate the center of the current window
set windowSizeX to item 1 of windowSize
set windowSizeY to item 2 of windowSize
set windowCenterX to windowSizeX / 2
set windowCenterY to windowSizeY / 2

-- calculate the center of the screen
set screenCenterX to (item 3 of screenBounds) / 2
set screenCenterY to (item 4 of screenBounds) / 2

--calculate the new window position
set newWindowPositionX to screenCenterX - windowCenterX
set newWindowPositionY to screenCenterY - windowCenterY

-- set the new window position
tell application "System Events"
    set position of frontWindow to {newWindowPositionX, newWindowPositionY}
end tell
    
posta bobtiki 26.11.2018 - 01:53
fonte

1 risposta

1

La risposta era consentire a SystemUIServer.app di controllare il computer, nella Sicurezza > Privacy > Preferenze di accessibilità . Ho avuto così tante finestre del Finder aperte, posso solo presumere che il dialogo di avvertimento si stesse facendo dietro a loro. Una volta concesso il permesso, funziona perfettamente.

    
risposta data 26.11.2018 - 05:00
fonte

Leggi altre domande sui tag