So it has been decades since I have been programming but it was by accident that while chatting with ChatGPT that I found that it had the ability to lead me by the hand to compile .CPP code, as well as prototyping a lot of apps that I only had been dreaming about but now with the help of an AI I was surprised to learn that AI can code well along with the help of a human of course. In fact the first time I did it,
I actually argued with the AI on what can and cannot be done and it is usually my tenacity that got me through the argument because ultimately AI doesn't write code well enough to run and it really needs a lot of human help.
Anyways I would like to post up my Automator Safari windows app here on this forum to share with you my journey back to writing code for the Mac and with every step of learning I do want to get back into writing great apps to share, to sell? or just to write great programs mainly to start with me to use.
So why write an Automator App for starting up Safari when you can just click the safari icon on the far left and you get a web window? well one of the things I would like an automator app to do is to simply start up another safari window whenever the app icon is click rather than right clicking the safari icon and selecting new window or new private, so much easier to just click on a new Safari window once and a new window pops up.... and that is generally all I wanted.... one click safari window pop up....
and perhaps some automated keys that gets rid of that chrome window advertisement pop up that takes over your search bar so that when your safari window opens and without looking you begin to type your search parameters to find that nothing went into the search bar because of that nasty chrome pop up.... .... so nothing more to add to the automator app ....
and perhaps if you drop a bunch of urls into the app it would open them all up in tabs.....
and lastly if you click on the new private safari icon you get a private safari window....
so not a lot to really program but there were a lot of caveats.... so lets begin....
so lets see if I can paste the automator apple script code into this window... and I'll chat with you about each section of the code...
So that is the whole code... save it as either "New Safari.app" or "New Private.app" and depending on the app name the appropriate window will open... You can drop a bunch of URL into the app and the safari window will pop them open in tabs. You can add this to Quick Actions and then select a bunch of url and right click and do quick actions on the url ...
One thing for sure is over the time that I spent learning from AI there were so many moments that AI will give you some code and when it doesn't work... and sometimes the code that did work, the AI would change it on you... or me... and if you blatantly just copy all the code into your script and suddenly it doesn't work... I would spend the next 8 hours arguing with the AI that it did work and the AI will tell you no you are wrong .... because apple doesn't allow keystrokes in Safari anymore????? say what? and when you ask silly questions like can I dismiss the google promo when a safari window pops up and ChatGPT would say "no because Google Promo isn't under the control of the safari window.... blah blah blah... and basically ChatGPT says no.... until a few days later.... you ask the innocent question... can we determine if the focus and cursor is on the search bar.... and Chat GPT would say Sure here is the detection code.... and then you add this gem of a code to your own code and sure enough it detects whether your google search has the focus.... and then you find through a lot of testing of loops to find that google steals the focus about .3 seconds after the window opens and then of course you send the keystrokes of "tab" and "space" to get rid of the google promo... and then voila.... the google promo is gone..... the thing is on a standard safari window, google promo isn't there.... only sometimes especially after you clear the cache.... so the problem was that if you send the keystrokes of "tab" and "space" on a window without the google promo, you will hit the microphone activation.... of which I had another block of code to dismiss the microphone pop up....... pulling my hair out on this one.... but in the end.... you see what you see.... a loop waiting for the focus to be stolen from the search bar.... just wait for it.... and once the focus is stolen then you simply send tab and space...
My first post here.... and if this post is acceptable ... there are so many wild stories... about my AI and I.... and programming that I would love to share.... but.... if not.... I will ask AI where to post next....
Thanks for reading.... let me know if you like this or not...
I actually argued with the AI on what can and cannot be done and it is usually my tenacity that got me through the argument because ultimately AI doesn't write code well enough to run and it really needs a lot of human help.
Anyways I would like to post up my Automator Safari windows app here on this forum to share with you my journey back to writing code for the Mac and with every step of learning I do want to get back into writing great apps to share, to sell? or just to write great programs mainly to start with me to use.
So why write an Automator App for starting up Safari when you can just click the safari icon on the far left and you get a web window? well one of the things I would like an automator app to do is to simply start up another safari window whenever the app icon is click rather than right clicking the safari icon and selecting new window or new private, so much easier to just click on a new Safari window once and a new window pops up.... and that is generally all I wanted.... one click safari window pop up....
and perhaps some automated keys that gets rid of that chrome window advertisement pop up that takes over your search bar so that when your safari window opens and without looking you begin to type your search parameters to find that nothing went into the search bar because of that nasty chrome pop up.... .... so nothing more to add to the automator app ....
and perhaps if you drop a bunch of urls into the app it would open them all up in tabs.....
and lastly if you click on the new private safari icon you get a private safari window....
so not a lot to really program but there were a lot of caveats.... so lets begin....
so lets see if I can paste the automator apple script code into this window... and I'll chat with you about each section of the code...
Code:
on run {input, parameters}
tell application "Safari" to launch
-- This was probably the hardest bit of code to find as AI will tell you that you always need two safari windows in
-- order to open a private Safari window... why? one window to give you the menu of new window and new private --- window..... well it turns out after a lot of testing
-- you do not need to activate a safari window to get the menu.... you use launch... and trust me I had to get AI to
-- tell me all the safari functions and tested every one of them to see if a window pops up or not.... and launch does
-- not pop up a window but it launches Safari if you had quit safari previously..... and then the menu becomes
-- available for the app to select new window or new private window.
set appPath to POSIX path of (path to me)
set appName to do shell script "basename " & quoted form of appPath & " .app"
-- Decide menu item based on app name
set menuItemName to "New Window"
if appName contains "Privat" then
set menuItemName to "New Private Window"
end if
-- wondering what I am doing here? well after the discovery that I don't have to create multiple windows in order to
-- create a private windowthe same code can be used for a normal safari web window or a private safari window. the
-- code is identical. Previous creation of a private window would be to activate a safari window and then use the
-- window menu to select new private .... and then use different methods including how many windows were open
-- before and after... so we could close the extra windows... believe me this was a mess.
-- okay... so I am using one set of code for both a new safari window or a new private window depending on the
-- name of the app. I just duplicate the .app in finder and rename the apps to New Safari.app and New Private.app...
-- and that piece of code above will determine what kind of safari window to open..... right?
repeat
try
tell application "System Events" to tell process "Safari"
click menu item menuItemName of menu "File" of menu bar 1
end tell
exit repeat
on error
delay 0.1
end try
end repeat
-- This is nice and tight... and brave.... because launching a safari session doesn't mean the menu is up and
-- running... originally I tried detecting whether safari is up and running and the checks all came back that it was....
-- I even tried pgrep and it too says safari is running
-- so .... there was nothing to do but try clicking the menu and if it gives us an error... just delay it by .1 second and
-- try again. This would be an infinite loop under normal circumstances ... but hey... this is only an automator app to
-- start up safari... and it doesn't delete or time out your system... small price to pay.... since there isn't any other
-- way to test out whether safari is ready for menu selection .... so we simply loop and try...
tell application "Safari" to activate
-- delay 1.6
-- Main loop: wait up to 1.6 seconds (16 x 0.1s) for focus to come back
set maxTries to 8
repeat with i from 1 to maxTries
if isSearchBarFocused() then exit repeat
delay 0.1
end repeat
-- Wait for focus to be lost Google Promo steals it from you about .3 seconds
-- then once you get your focus back you wait until google promo steals it from you
repeat with j from 1 to maxTries
if not isSearchBarFocused() then
dismissGooglePromo()
exit repeat
end if
delay 0.1
end repeat
-- and then once google steals it from you, now you can send the keyboard "tab" and "space" to get rid of promo
set maxTabs to 10
set fileCount to count of input
if fileCount > maxTabs then
display dialog "Too many files (" & fileCount & "). Only processing first " & maxTabs & "."
set fileCount to maxTabs
end if
repeat with i from 1 to fileCount
set theFile to item i of input
try
tell application "Safari" to open location (do shell script "plutil -extract URL raw " & quoted form of POSIX path of theFile)
on error errMsg
display dialog "Error opening file: " & (theFile as text) & return & errMsg
end try
end repeat
return input
end run
-- Handler to check if the focus is on Google's main search bar
on isSearchBarFocused()
tell application "Safari"
set focusInfo to do JavaScript "
var el = document.activeElement;
el ? (el.id || el.name || el.tagName || '') : '';
" in front document
end tell
return (focusInfo is "q" or focusInfo is "APjFqb")
end isSearchBarFocused
-- Handler to dismiss the Google Chrome promo
on dismissGooglePromo()
tell application "System Events"
keystroke tab
delay 0.1
keystroke space
end tell
delay 0.2 -- Allow UI to update
end dismissGooglePromo
So that is the whole code... save it as either "New Safari.app" or "New Private.app" and depending on the app name the appropriate window will open... You can drop a bunch of URL into the app and the safari window will pop them open in tabs. You can add this to Quick Actions and then select a bunch of url and right click and do quick actions on the url ...
One thing for sure is over the time that I spent learning from AI there were so many moments that AI will give you some code and when it doesn't work... and sometimes the code that did work, the AI would change it on you... or me... and if you blatantly just copy all the code into your script and suddenly it doesn't work... I would spend the next 8 hours arguing with the AI that it did work and the AI will tell you no you are wrong .... because apple doesn't allow keystrokes in Safari anymore????? say what? and when you ask silly questions like can I dismiss the google promo when a safari window pops up and ChatGPT would say "no because Google Promo isn't under the control of the safari window.... blah blah blah... and basically ChatGPT says no.... until a few days later.... you ask the innocent question... can we determine if the focus and cursor is on the search bar.... and Chat GPT would say Sure here is the detection code.... and then you add this gem of a code to your own code and sure enough it detects whether your google search has the focus.... and then you find through a lot of testing of loops to find that google steals the focus about .3 seconds after the window opens and then of course you send the keystrokes of "tab" and "space" to get rid of the google promo... and then voila.... the google promo is gone..... the thing is on a standard safari window, google promo isn't there.... only sometimes especially after you clear the cache.... so the problem was that if you send the keystrokes of "tab" and "space" on a window without the google promo, you will hit the microphone activation.... of which I had another block of code to dismiss the microphone pop up....... pulling my hair out on this one.... but in the end.... you see what you see.... a loop waiting for the focus to be stolen from the search bar.... just wait for it.... and once the focus is stolen then you simply send tab and space...
My first post here.... and if this post is acceptable ... there are so many wild stories... about my AI and I.... and programming that I would love to share.... but.... if not.... I will ask AI where to post next....
Thanks for reading.... let me know if you like this or not...
Last edited: