Our Automator, manage macOS applications in AppleScript

One wonderful evening, my colleague and I published a small application in the AppStore. Publishing an application is a rather long process and consists of many stages. One of the stages is preparing images for the app store. The task, at first glance, is simple - to launch the application in the simulator and take a screenshot of the application, but we need screens in six languages, in several sizes, with a demonstration of five different states of the application. For an hour, you could manage just by taking pictures with your hands, while drinking coffee and discussing general topics. But we are programmers and it is not our method to do it by hand. We need to automate the process. Even though we never did that, we did it. We learned how easy it is to programmatically manage macOS applications. And they wrote AppleScript that runs the XCode and Simulator apps.





Staging

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





Of course, we could also automate the copying of files, but we stopped there.





Outcome

Instead of making all 120 pictures with our hands in about an hour, we learned how to use Automator, having spent three hours mastering the program and the AppleScript language, and our script allowed us to generate 120 pictures per minute with a minimum of operations. Despite the long investment of time, we were satisfied. I hope our experience can be useful for other people and for other tasks.





And here is the code in full:





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