Background: I am creating automation of my phone that only allows apps to be run based on certain criteria. This automation is for the purpose of preventing bad habits like constant social media use. E.G. my automation would block accessing Instagram multiple times within a 30 minute time period. It would initially allow Instagram at 10:00 pm then if I try to open Instagram from 10:00 pm to 10:29 pm it would immediately close the app thus preventing me from using Instagram too much.
Code and comments: the ios automation setup
However with the automation in shortcuts on the iPhone, I can only know what app is open if I create individual automation for EVERY SINGLE APP on my phone and hardcode a name. This is extremely impractical especially since I want to share this automation and code with friends that also want to cut back on phone usage. list of apps that trigger this automation when opened.
The code to be run after the automation is triggered (any app is opened). Calls a Pythonista script that retrieves the name of the most recently opened app E.G. Instagram and pastes it to the clipboard. The shortcuts code then retrieves the name from the clipboard and pastes it to a shortcut that determines whether the app is allowed to be used at this time.
the code: 1st part of ios shortcuts code to be run on automation trigger
2nd part of ios shortcuts code
My question is how can I return the name of the most recently opened app within a Pythonista script. I am open to answers that incorporate different languages like Swift or javascript but I have no experience with those and I don't know how to incorporate them into my automation like I can call Pythonista scripts within ios shortcuts.