Last active
April 27, 2024 03:35
-
-
Save AT5HK/f8714254dee430cfbc90c47fe0f1051b to your computer and use it in GitHub Desktop.
bash script to automate ios simulator screenshots for the app store. Enter app {bundle identifier} {bundle location} as of this writing the bundle location is in your derived data for the simulator (build with xcode first) or your project directory under build, depending on how you built it. #This script can only take screenshots of the first pa…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
set -e | |
# Set the bundle identifier of the app you want to install | |
APP_BUNDLE_IDENTIFIER=$1 | |
# the location of your simulator app bundle. Should be in derived data after you build it on xcode for a simulator | |
APP_BUNDLE_LOCATION=$2 | |
DIR_NAME=screenshots_$APP_BUNDLE_IDENTIFIER | |
#simulator phone identifiers for appstore previews | |
#6.7inch | |
iPhone_14_Pro_Max=4B8C6A2D-AAD9-4EB0-988D-BC82300C9FBC | |
#6.5inch | |
iPhone_14_Plus=FDBF52D7-24D1-4FC1-B60F-743637F79417 | |
#5.5inch | |
iPhone_8_Plus=DAAEACEC-A273-468A-B479-22B6AEDAB625 | |
#12.9inch | |
iPad_Pro_6th_generation=FB21C32F-45FE-4695-A8D9-4F225EA5A840 | |
#12.9inch | |
iPad_Pro_2nd_generation=A614BE44-23ED-4AC3-8A70-1F1C2E655AEF | |
version="1.1" | |
# ============= print help ======================================================================= | |
function print_help { | |
cat << EOF | |
Usage: $0 [-hv] [APP_BUNDLE_IDENTIFIER] [APP_BUNDLE_LOCATION] | |
Creates a screenshot directory [screenshots_APP_BUNDLE_IDENTIFIER] in your current directory | |
Options: | |
-h, --help Print this help | |
-v, --version Current version | |
EOF | |
} | |
function exit_script { | |
exit_code=$1 | |
exit $exit_code | |
} | |
function show_syntax { | |
echo $0 [-hv] [APP_BUNDLE_IDENTIFIER] [APP_BUNDLE_LOCATION] | |
} | |
function show_version { | |
echo $0 current version $version | |
} | |
case "$1" in | |
"--help" | "-h") | |
print_help | |
exit_script 0 | |
;; | |
"--version" | "-v") | |
show_version | |
exit_script 0 | |
;; | |
*) | |
#show_version | |
;; | |
esac | |
if ! [ -n "$APP_BUNDLE_IDENTIFIER" ]; then | |
print_help | |
exit_script 1 | |
fi | |
if ! [ -d "$APP_BUNDLE_LOCATION" ]; then | |
echo Error App Bundle not found | |
exit_script 1 | |
fi | |
# ============= script functions ======================================================================= | |
function nameForID { | |
if [[ "$1" == "$iPhone_14_Pro_Max" ]]; then | |
echo iPhone_14_Pro_Max | |
elif [[ "$1" == "$iPhone_14_Plus" ]]; then | |
echo iPhone_14_Plus | |
elif [[ "$1" == "$iPhone_8_Plus" ]]; then | |
echo iPhone_8_Plus | |
elif [[ "$1" == "$iPad_Pro_6th_generation" ]]; then | |
echo iPad_Pro_6th_generation | |
elif [[ "$1" == "$iPad_Pro_2nd_generation" ]]; then | |
echo iPad_Pro_2nd_generation | |
else | |
echo error this should not happen | |
fi | |
} | |
#========================= | |
# $1 is the parameter passed into this function and called at the bottom of this script. These are not the script paramaters | |
function bootSimAndGetScreenshot { | |
IPHONE_NAME=$(nameForID $1) | |
echo the iphone name is $IPHONE_NAME | |
#check if given simulator is booted or boot it | |
if xcrun simctl bootstatus $1 -b; then | |
echo installing app for $1 | |
#install app | |
xcrun simctl install $1 $APP_BUNDLE_LOCATION | |
#open app | |
xcrun simctl launch $1 $APP_BUNDLE_IDENTIFIER | |
#take screenshot | |
sleep 4 | |
xcrun simctl io $1 screenshot $DIR_NAME/$IPHONE_NAME.png | |
#shutdown device simulator | |
xcrun simctl shutdown $1 | |
else | |
echo failed to launch simulator $1 | |
fi | |
} | |
# seems all the commands wait to finish to run the next unlike swift | |
echo $? | |
#pgrep is case sensitive | |
if pgrep -x "Simulator" | |
then | |
echo "Simulator already running" | |
else | |
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app | |
echo "Simulator not running, opening Simulator" | |
fi | |
#overwrites any directory with the same name | |
mkdir -p $DIR_NAME | |
bootSimAndGetScreenshot $iPhone_8_Plus | |
bootSimAndGetScreenshot $iPhone_14_Pro_Max | |
bootSimAndGetScreenshot $iPhone_14_Plus | |
bootSimAndGetScreenshot $iPad_Pro_2nd_generation | |
bootSimAndGetScreenshot $iPad_Pro_6th_generation | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment