Automate Screenshots on iPhone Simulator?

后端 未结 6 1662
耶瑟儿~
耶瑟儿~ 2020-12-23 02:13

I\'m tired of taking new screenshots everytime I change my UI for my iPhone application. I would like to be able to run a script/program/whatever to load my binary on the si

6条回答
  •  抹茶落季
    2020-12-23 02:30

    I have the same wish. I want to be able to save screenshots from a few screens in my app without all the manual work. I'm not there yet, but I have started.

    The idea is to tail /var/log/system.log, where the output from the NSLog statements go. I pipe the output to a python program. The python program reads all the lines from stdin and when the line matches a specific pattern, it calls screencapture.

    NSLog(@"screenshot mainmenu.png");
    

    That will cause a screenshot named "XX. mainmenu YY.png" to be created every time it is called. XX is the number of the screenshot since the program started. YY is the number of the "mainmenu" screenshot.

    I have even added some unnecessary features:

    NSLog(@"screenshot -once mainmenu.png");
    

    This will only save "XX. mainmenu.png" once.

    NSLog(@"screenshot -T 4 mainmenu.png");
    

    This will make the screenshot after a delay of 4 seconds.

    After running an app with the right logging, screenshots with the following names could have been created:

    00. SplashScreen.png
    01. MainMenu 01.png
    03. StartLevel 01.png
    04. GameOver 01.png
    05. MainMenu 02.png
    

    Give it a try:

    1. Add some NSLog statements to your code

    2. $ tail -f -n0 /var/log/system.log | ./grab.py

    3. Start your iPhone app in the Simulator

    4. Play around with your app

    5. Have a look at the screenshots showing up where you started the grab.py program

    grab.py:

    #!/usr/bin/python
    
    import re
    import os
    from collections import defaultdict
    
    def screenshot(filename, select_window=False, delay_s=0):
        flags = []
        if select_window:
            flags.append('-w')
        if delay_s:
            flags.append('-T %d' % delay_s)
        command_line = 'screencapture %s "%s"' % (' '.join(flags), filename)
        #print command_line
        os.system(command_line)
    
    def handle_line(line, count=defaultdict(int)):
        params = parse_line(line)
        if params:
            filebase, fileextension, once, delay_s = params
            if once and count[filebase] == 1:
                print 'Skipping taking %s screenshot, already done once' % filebase
            else:
                count[filebase] += 1
                number = count[filebase]
                count[None] += 1
                global_count = count[None]
                file_count_string = (' %02d' % number) if not once else ''
    
                filename = '%02d. %s%s.%s' % (global_count, filebase, file_count_string, fileextension)
                print 'Taking screenshot: %s%s' % (filename, '' if delay_s == 0 else (' in %d seconds' % delay_s))
                screenshot(filename, select_window=False, delay_s=delay_s)
    
    def parse_line(line):
        expression = r'.*screenshot\s*(?P-once)?\s*(-delay\s*(?P\d+))?\s*(?P\w+)?.?(?P\w+)?'
        m = re.match(expression, line)
        if m:
            params = m.groupdict()
            #print params
            filebase = params['filebase'] or 'screenshot'
            fileextension = params['fileextension'] or 'png'
            once = params['once'] is not None
            delay_s = int(params['delay_s'] or 0)
            return filebase, fileextension, once, delay_s
        else:
            #print 'Ignore: %s' % line
            return None
    
    def main():
        try:
            while True:
                handle_line(raw_input())
        except (EOFError, KeyboardInterrupt):
            pass
    
    if __name__ == '__main__':
        main()
    

    Issues with this version:

    If you want to take a screenshot of only the iPhone Simulator window, you have to click the iPhone Simulator window for every screenshot. screencapture refuses to capture single windows unless you are willing to interact with it, a strange design decision for a command line tool.

    Update: Now iPhone simulator cropper (at http://www.curioustimes.de/iphonesimulatorcropper/index.html) works from command line. So instead of using the built in screencapture, download and use it instead. So now the process is completely automatic.

提交回复
热议问题