#!/bin/sh cd ~/Library/Developer/CoreSimulator/Devices/ isVaildDeviceDir () { # echo $1 # $1 is not directory or empty if ! [[ -d $1 ]] || ! [[ -n $1 ]] ; then return 1 fi device_type=$(/usr/libexec/PlistBuddy -c "Print :deviceType" "$1/device.plist") # Skip apple watch device if [[ $device_type == com.apple.CoreSimulator.SimDeviceType.Apple-Watch* ]]; then return 1 fi return 0 } for f in $(ls -t) do if isVaildDeviceDir $f; then break fi done cd $f device_dir=$PWD echo $device_dir device_type=$(/usr/libexec/PlistBuddy -c "Print :deviceType" "device.plist") device_name=$(/usr/libexec/PlistBuddy -c "Print :name" "device.plist") echo "The latest updated iOS device is: $device_name ($device_type)" # Find latest app bundle dir cd "$device_dir/data/Containers/Bundle/Application" app_bundle_dir=$(ls -t|head -1) if [[ -z $app_bundle_dir ]]; then echo "Seems ths device doesnt have any application instead. $PWD" exit 1 fi # Get the bundle, and id cd $app_bundle_dir app_bundle_dir= for f in *.app do app_bundle_dir=$f done if [[ -z $app_bundle_dir ]]; then echo "Cannot find a application bundle at $PWD" exit 1 fi cd "$app_bundle_dir" app_bundle_dir="$PWD" app_bundle_id=$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "Info.plist") echo "The latest updated application is: $app_bundle_id @$app_bundle_dir" if [[ -z $app_bundle_id ]]; then echo "Error: invaild bundle id" exit 1 fi # Find the right data container cd "$device_dir/data/Containers/Data/Application" isContainerForID() { if ! [[ -n $1 ]] || ! [[ -n $2 ]] ; then return 1 fi local metadata="$1/.com.apple.mobile_container_manager.metadata.plist" if ! [ -f $metadata ]; then return 1 fi local current_id=$(/usr/libexec/PlistBuddy -c "Print :MCMMetadataIdentifier" "$metadata") if ! [[ $current_id == $2 ]]; then return 1 fi return 0 } for f in $(ls -t) do if isContainerForID $f $app_bundle_id; then cd $f echo "The latest application’s data container is: $PWD" open $PWD exit 0 fi done echo "Cannot find the latest application’s data container." exit 1