Nosso Automator, gerencia aplicativos macOS em AppleScript

Em uma noite maravilhosa, meu colega e eu publicamos um pequeno aplicativo na AppStore. Publicar um aplicativo é um processo bastante longo e consiste em várias etapas. Uma das etapas é a preparação de imagens para a app store. A tarefa, à primeira vista, é simples - lançar o aplicativo no simulador e fazer uma captura de tela do aplicativo, mas precisamos de telas em seis idiomas, em vários tamanhos, com demonstração de cinco diferentes estados do aplicativo. Por uma hora, você poderia fazer isso apenas tirando fotos com as mãos, enquanto bebia café e discutia assuntos gerais. Mas somos programadores e não é nosso método fazê-lo manualmente. Precisamos automatizar o processo. Mesmo que nunca tenhamos feito isso, nós o fizemos. Aprendemos como é fácil gerenciar aplicativos macOS de maneira programática. E eles escreveram AppleScript que executa os aplicativos XCode e Simulator.





Encenação

. 6 , iPhone iPad. - . , . iPhone , iPad , .





Automator.





.





WorkFlow - . . Actions - . WorkFlow. WorkFlow . WorkFlow.





, , - , . . Automator . WorkFlow, - Run AppleScript.





, , Run AppleScript, , Run JavaScript Run Shell Script. WorkFlow , Workflow (Run WorkFlow).





Run AppleScript , . . . AppleScript. - .





, , .





XCode. - . Xcode , Xcode , .





sizes.





set ipad to "iPad Pro (12.9-inch) (3rd generation)"
set sizes to {"iPhone 8 Plus", "iPhone 11 Pro Max", ipad}
      
      



schemes





set schemes to {"TinyApp", "TinyApp-cn", "TinyApp-jp", "TinyApp-es", "TinyApp-de", "TinyApp-ru", "TinyApp-fr"}
      
      



:





repeat with size in sizes
    repeat with lang in schemes
    		-- .....
		end repeat
end repeat

      
      



size, lang.





XCode , , Simulator:





        tell application "Xcode" to activate
        tell application "System Events"
            tell process "Xcode"
                tell menu bar 1
                    tell menu "Product"
                        tell menu item "Scheme"
                            tell menu "Scheme"
                                click menu item lang
                            end tell
                        end tell   
                        tell menu item "Destination"
                            tell menu "Destination"
                                click menu item size
                            end tell
                        end tell
                        click menu item "Run"
                    end tell
                end tell
            end tell
        end tell

      
      



:





        tell application "System Events"
            display dialog "Continue"
        end tell

      
      



, , () Continue. , Continue.





, .





        tell application "Automator" to activate
        tell application "System Events"
            tell process "Simulator"
                tell menu bar 1
                    tell menu "File"
                        click menu item "Save Screen"
                    end tell
                end tell
            end tell
        end tell

      
      



:





        tell application "Finder"
            set the source_folder to (path to desktop folder) as alias
            sort (get files of source_folder) by creation date
            set theFile to (item 1 of reverse of result) as alias
            set newName to lang & "-" & size & " .png"
            set name of theFile to newName
        end tell

      
      



iPad :





        if size as string is equal to ipad then
            tell application "Automator" to activate
            tell application "System Events"
                tell process "Simulator"
                    tell menu bar 1
                        
                        tell menu "Hardware"
                            tell menu item "Orientation"
                                tell menu "Orientation"
                                    click menu item "Landscape Right"
                                end tell
                            end tell
                        end tell
                        
                        delay 2
                        tell menu "File"
                            click menu item "New Screen Shot"
                        end tell
                        
                        tell application "Finder"
                            set the source_folder to (path to desktop folder) as alias
                            sort (get files of source_folder) by creation date
                            set theFile to (item 1 of reverse of result) as alias
                            set newName to lang & "-" & size & "-landscape" & " .png"
                            set name of theFile to newName
                        end tell                        
                        
                        tell menu "Hardware"
                            tell menu item "Orientation"
                                tell menu "Orientation"
                                    click menu item "Portrait"
                                end tell
                            end tell
                        end tell
                        
                    end tell
                end tell
            end tell
        end if

      
      



. , . , . - AppStore.





Claro, também podíamos automatizar a cópia de arquivos, mas paramos por aí.





Resultado

Em vez de fazer todas as 120 fotos com as mãos em cerca de uma hora, aprendemos a usar o Automator, tendo passado três horas dominando o programa e a linguagem AppleScript, e nosso script nos permitiu gerar 120 fotos por minuto com um mínimo de operações. Apesar do longo investimento de tempo, ficamos satisfeitos. Espero que nossa experiência possa ser útil para outras pessoas e para outras tarefas.





E aqui está o código completo:





on run {input, parameters}
    set ipad to "iPad Pro (12.9-inch) (3rd generation)"
    set sizes to {"iPhone 8 Plus", "iPhone 11 Pro Max", ipad}
    set schemes to {"TinyApp", "TinyApp-cn", "TinyApp-jp", "TinyApp-es", "TinyApp-de", "TinyApp-ru", "TinyApp-fr"}
    repeat with size in sizes
        repeat with lang in schemes
            tell application "Xcode" to activate
            tell application "System Events"
                tell process "Xcode"
                    tell menu bar 1
                        tell menu "Product"
                            
                            tell menu item "Scheme"
                                tell menu "Scheme"
                                    click menu item lang
                                end tell
                            end tell
                            
                            tell menu item "Destination"
                                tell menu "Destination"
                                    click menu item size
                                end tell
                            end tell
                            click menu item "Run"
                        end tell
                    end tell
                end tell
            end tell
            
            tell application "System Events"
                display dialog "Continue"
            end tell
            
            tell application "Automator" to activate
            tell application "System Events"
                tell process "Simulator"
                    tell menu bar 1
                        tell menu "File"
                            click menu item "Save Screen"
                        end tell
                    end tell
                end tell
            end tell
            
            tell application "Finder"
                set the source_folder to (path to desktop folder) as alias
                sort (get files of source_folder) by creation date
                set theFile to (item 1 of reverse of result) as alias
                set newName to lang & "-" & size & " .png"
                set name of theFile to newName
            end tell
            
            
            --iPad
            if size as string is equal to ipad then
                
                
                tell application "Automator" to activate
                tell application "System Events"
                    tell process "Simulator"
                        tell menu bar 1
                            
                            tell menu "Hardware"
                                tell menu item "Orientation"
                                    tell menu "Orientation"
                                        click menu item "Landscape Right"
                                    end tell
                                end tell
                            end tell
                            
														delay 2
                            tell menu "File"
                                click menu item "New Screen Shot"
                            end tell
                            
                            tell application "Finder"
                                set the source_folder to (path to desktop folder) as alias
                                sort (get files of source_folder) by creation date
                                set theFile to (item 1 of reverse of result) as alias
                                set newName to lang & "-" & size & "-landscape" & " .png"
                                set name of theFile to newName
                            end tell                  
                            
                            tell menu "Hardware"
                                tell menu item "Orientation"
                                    tell menu "Orientation"
                                        click menu item "Portrait"
                                    end tell
                                end tell
                            end tell
                            
                        end tell
                    end tell
                end tell
            end if
            
            
        end repeat
    end repeat
    
    
    return input
end run
      
      






All Articles