Skip to content

Instantly share code, notes, and snippets.

@skagedal

skagedal/simulator.zsh

Last active Feb 15, 2021
Embed
What would you like to do?
Extension for zsh that gives an auto-completable cd to a simulator data container of the app with the given bundle identifier.
# Extension for zsh that cd to a simulator data container of the app with the given bundle identifier.
#
# See blog post at https://skagedal.github.io/2018/01/02/simcd.html
function simdir () {
xcrun simctl get_app_container booted $1 data
}
function simcd () {
cd `simdir $1`
}
function simpushd () {
pushd `simdir $1`
}
function simopen() {
open `simdir $1`
}
function _bundle_identifiers () {
xcrun simctl listapps booted | plutil -convert json - -o - | ruby -r json -e 'puts JSON.parse(STDIN.read).keys'
}
_simdir() {
local state
_arguments \
'1: :->bundle_identifier'\
'*: :->rest'
case $state in
(bundle_identifier) _arguments '1:Bundle identifier:($(_bundle_identifiers))' ;;
(*) compadd "$@"
esac
}
compdef _simdir simdir simcd simpushd simopen
@AliSoftware

This comment has been minimized.

Copy link

@AliSoftware AliSoftware commented Jan 3, 2018

Tip: to list the CFBundleIdentigiers, instead of using a grep + cut + grep hack, you could instead use this, to convert the PLIST to a proper JSON then use ruby to parse and extract the keys from the JSON. Returns the same list of identifiers, but much cleaner.

xcrun simctl listapps booted | plutil -convert json - -o - | ruby -r json -e 'puts JSON.parse(STDIN.read).keys'

@skagedal

This comment has been minimized.

Copy link
Owner Author

@skagedal skagedal commented Jan 5, 2018

@AliSoftware Good call, post and gist updated!

@idcrook

This comment has been minimized.

Copy link

@idcrook idcrook commented Feb 14, 2021

thanks for this gist. if the jq utility is available, there is the even shorter:

xcrun simctl listapps booted | plutil -convert json - -o - | jq -r 'keys | .[]'
# or even 
xcrun simctl listapps booted | plutil -convert json - -o - | jq -r 'keys[]'
@skagedal

This comment has been minimized.

Copy link
Owner Author

@skagedal skagedal commented Feb 15, 2021

That's true, @idcrook! I wanted a solution that would work out of the box on a macOS machine though :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment