Nuestro Automator, gestiona aplicaciones macOS en AppleScript

Una noche maravillosa, mi colega y yo publicamos una pequeña aplicación en la AppStore. La publicación de una aplicación es un proceso bastante largo y consta de muchas etapas. Una de las etapas es la preparación de imágenes para la tienda de aplicaciones. La tarea, a primera vista, es simple: lanzar la aplicación en el simulador y tomar una captura de pantalla de la aplicación, pero necesitamos pantallas en seis idiomas, en varios tamaños, con una demostración de cinco estados diferentes de la aplicación. Durante una hora, podría arreglárselas simplemente tomando fotografías con las manos, mientras toma café y habla de temas generales. Pero somos programadores y no es nuestro método hacerlo a mano. Necesitamos automatizar el proceso. Aunque nunca hicimos eso, lo hicimos. Aprendimos lo fácil que es administrar mediante programación aplicaciones macOS. Y escribieron AppleScript que ejecuta las aplicaciones XCode y Simulator.





Puesta en escena

. 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.





Por supuesto, también podríamos automatizar la copia de archivos, pero nos detuvimos ahí.





Salir

En lugar de hacer las 120 imágenes con nuestras manos en aproximadamente una hora, aprendimos a usar Automator, después de haber pasado tres horas dominando el programa y el lenguaje AppleScript, y nuestro script nos permitió generar 120 imágenes por minuto con un mínimo de operaciones. A pesar de la larga inversión de tiempo, quedamos satisfechos. Espero que nuestra experiencia pueda ser de utilidad para otras personas y para otras tareas.





Y aquí está el 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