Notre Automator, gérez les applications macOS dans AppleScript

Une merveilleuse soirée, mon collègue et moi avons publié une petite application dans l'AppStore. La publication d'une application est un processus assez long et se compose de nombreuses étapes. L'une des étapes consiste à préparer les images pour l'App Store. La tâche, à première vue, est simple: lancer l'application dans le simulateur et prendre une capture d'écran de l'application, mais nous avons besoin d'écrans en six langues, en plusieurs tailles, avec une démonstration de cinq états différents de l'application. Pendant une heure, vous pourriez vous débrouiller simplement en prenant des photos avec vos mains, tout en buvant du café et en discutant de sujets généraux. Mais nous sommes des programmeurs et ce n'est pas notre méthode de le faire à la main. Nous devons automatiser le processus. Même si nous n'avons jamais fait cela, nous l'avons fait. Nous avons appris à quel point il est facile de gérer les applications macOS par programmation. Et ils ont écrit AppleScript qui exécute les applications XCode et Simulator.





Mise en scène

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





Bien sûr, nous pourrions aussi automatiser la copie des fichiers, mais nous nous sommes arrêtés là.





Résultat

Au lieu de faire les 120 images avec nos mains en une heure environ, nous avons appris à utiliser Automator, après avoir passé trois heures à maîtriser le programme et le langage AppleScript, et notre script nous a permis de générer 120 images par minute avec un minimum d'opérations. Malgré le long investissement en temps, nous avons été satisfaits. J'espère que notre expérience pourra être utile pour d'autres personnes et pour d'autres tâches.





Et voici le code en entier:





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