Skip to content

Instantly share code, notes, and snippets.

@jroehl
Last active November 22, 2019 10:12
Show Gist options
  • Save jroehl/e5230754a95d7c243a3c6290668d94fb to your computer and use it in GitHub Desktop.
Save jroehl/e5230754a95d7c243a3c6290668d94fb to your computer and use it in GitHub Desktop.

Interactive bash script to open the sqlite database directory (core data) of a specific app installed in Xcode simulator

Needs jq to be installed
Needs xcrun to be installed (xcode-select --install)
Needs /usr/libexec/PlistBuddy to be installed (xcode-select --install)

Usage

. <(curl -s https://gist.githubusercontent.com/jroehl/e5230754a95d7c243a3c6290668d94fb/raw/find-simulator-coredata.sh)

#!/bin/sh
command -v jq >/dev/null 2>&1 || {
echo >&2 "Script needs jq to be installed. Aborting."
exit 1
}
command -v xcrun >/dev/null 2>&1 || {
echo >&2 "Script needs xcrun to be installed. Aborting."
exit 1
}
command -v /usr/libexec/PlistBuddy >/dev/null 2>&1 || {
echo >&2 "Script needs PlistBuddy to be installed. Aborting."
exit 1
}
main() {
available_devices=$(xcrun simctl list devices available -j)
SAVEIFS=${IFS}
IFS=$'\n'
devices=($(echo "${available_devices}" | jq -r '.devices | flatten | sort_by(.state) | .[] | "\(.state) > \(.name) [\(.udid)]"'))
IFS=${SAVEIFS}
device_path="${HOME}/Library/Developer/CoreSimulator/Devices"
echo "\nAvailable devices:"
select device in "${devices[@]}"; do
echo "\nUsing device \"${device}\""
device_id=$(echo $device | sed 's/.*\[\([0-9A-Z\-]*\)\].*$/\1/g')
device_path+="/${device_id}/data/Containers/Data/Application"
break
done
bundle_identifiers=()
for plist in ${device_path}/*/.*.plist; do
bundle_identifier=$(/usr/libexec/PlistBuddy -c 'print ":MCMMetadataIdentifier"' ${plist})
if [[ ${bundle_identifier} != com.apple.* ]]; then
bundle_identifiers+=(${bundle_identifier})
fi
done
echo "\nAvailable custom bundle identifiers:"
select bundle_identifier in "${bundle_identifiers[@]}"; do
echo "\nGetting info for bundle \"${bundle_identifier}\"\n"
data_path=$(xcrun simctl get_app_container booted ${bundle_identifier} data)
db_path="${data_path}/Library/Application Support"
xcrun simctl appinfo booted ${bundle_identifier}
echo "\nOpening sqlite path in finder"
open -a finder "${db_path}"
break
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment