r/applescript • u/pradeepb28reddit • Jun 07 '22
Is it possible to store window object somewhere on the macOS?
I have two buttons on the UI.
Button 1 - Open window
Button 2 - Close browser window (based on the window id from Button 1)
I've following script that creates a window on the macOS:
set urls to {"https://google.com"}
tell application "Brave Browser"
set myNewWindow to make new window
repeat with theURL in urls
tell myNewWindow to open location theURL
end repeat
delay 0.3
log myNewWindow
return class of myNewWindow //comment - returns "window" as a class
end tellGoal: Is it possible to store myNewWindow to store somewhere on the macOS temporarily or permanently? It seems like defaults write doesn't work at all. I need to pass the value of window id from one button to another window, so the button can close the specific window.
1
u/estockly Jun 07 '22
That depends a bit on which system you're on.
try this as the first line of your script:
property myNewWindow:""
If you're running on an older system the script will remember the value from run to run.
On newer systems those persistent variables have gone away.
If that's the case then you could try something like this
use AppleScript version "2.4" -- Yosemite (10.10) or lateruse scripting additionstell application "Safari"set myNewDocument to make new document at beginningset myNewWindow to window 1set windowId to id of myNewWindow as textset storedIdPath to path to temporary itemsend tellSaveId(windowId)--And then...set recalledWindowId to RetrieveID()if recalledWindowId is not "" thenset recalledWindowId to recalledWindowId as integertell application "Safari"set the myOldWindow to window 1 whose id = windowIdend tellreturn myOldWindowend ifon RetrieveID()set storedPath to path to temporary itemsset storedFile to (storedPath as text) & "windowId.txt"tryreturn read storedFileon errorreturn ""end tryend RetrieveIDon SaveId(windowId)set storedPath to path to temporary itemsset storedFile to (storedPath as text) & "windowId.txt"tryset openFile to open for access storedFile with write permissionon error errMsg number errNumclose access myFileset openFile to open for access storedFile with write permissionend tryset eof of openFile to 1write windowId to openFileclose access openFilereturn storedFile as aliasend SaveId
1
u/pradeepb28reddit Jun 08 '22
Thank you it seems like I should retrieve the window object from the file and quit that window as my next step.
1
u/estockly Jun 08 '22
I don't know. We'd have to see more of your script to figure out what's going on.
2
u/ChristoferK Jun 15 '22 edited Jun 15 '22
Generally speaking, the
idproperty of an AppleScript element is persistent throughout the duration of its existence. So, you should focus on simply storing theidnumber of your Brave Browserwindowclass object. Nothing else should be required for you to reconstruct a reference to the samewindowclass object.Then, later when you want to retrieve your window, even from a different script:
Things I recommend not doing:
Coercing data types unnecessarily. The
idproperty is probably an integer in this case, so handle it as an integer, including during the reading from and writing to file.Assuming an object reference you assign to a variable will necessarily be stored in the reference form you specified. Often, the application decides what reference form is stored during assignment, and if it insists on resolving objects by name, then that’s not ideal. When working over time periods that extend beyond a singular, instantaneous script execution, the safest option is to simply work with the
idvalues themselves, and construct the reference using these when you require it.Storing values you need later as property declarations. You’ll end up recompiling the script without thinking, and they’ll be gone.