I'm trying to select a certain terminal tab in OSX and send keystrokes to it. But Terminal in OSX 10.8.4 doesn't seem to store anything but "Terminal" for a tab's custom title, even if you set a custom title with the inspector. Any ideas? Here is the code I'd like to use to select the right tab:
tell application "Terminal"
set allWindows to number of windows
repeat with i from 1 to allWindows
set allTabs to number of tabs of window i
repeat with j from 1 to allTabs
if custom title of tab j of window i contains "blah" then
set frontmost of window i to true
set selected of tab j of window i to true
end if
end repeat
end repeat
end tell
I'm working on an older version of OSX, so I couldn't duplicate your problem (your code works fine for me), but perhaps try:
if ((custom title of (current settings of (tab j of (window i)))) as string) contains "blah" then
Make sure you set the tab name this way:
tell "application" "Terminal"
# ...
set the_tab to tab 1 of the front window -- replace with your tab selector
tell settings set "Basic"
set custom title of the_tab to "My Tab Name"
end tell
# ...
end tell
You should be able to get retrieve the tab name:
tell "application" "Terminal"
set the_name to custom title of the_tab as string
end tell
Works for me on OSX 10.8.4
Related
This code from here works perfect, but doesn't work in Firefox. Why and how to fix it in applescript?
tell application "Google Chrome"
repeat until (loading of tab 1 of window 1 is false)
1 + 1 --just an arbitary line
end repeat
loading of tab 1 of window 1 --this just returns final status
end tell
As Firefox does not contain a specific AppleScript dictionary, e.g. Firefox.sdef file, it is not considered to be AppleScript scriptable in the same way as e.g. Google Chrome, and while it will respond to some of the basic commands, such as activate, quit, etc., it will require UI Scripting to do what you are asking.
The following example AppleScript code requires Firefox version 87 or newer, and setting its accessibility.force_disabled preference to: -1
First, in Firefox, in the Address/Search combo box, type about:config and press enter.
If applicable, click the Accept the Risk and Continue button.
In the Search preference name text box, type accessibility.force_disabled and press enter.
Click the Edit button and change its value to: -1
Click the the Save button.
Here is an example of how I would instruct Firefox to open a new window to a given URL and wait for the page to finish loading.
Example AppleScript code:
set theURL to "https://news.google.com/"
tell application "Firefox" to activate
delay 0.5 -- # Value may need to be adjusted if Firefox is closed.
my clickApplicationMenuCommand("Firefox", "File", "New Window")
delay 0.5
my setURLofFirefoxFrontWindowTo(theURL)
my waitForFirefoxPageToFinishLoading()
say "foobar"
-- # Handler(s) #
to clickApplicationMenuCommand(appName, appMenuName, appMenuCommand)
tell application appName to activate
delay 0.25
tell application "System Events" to ¬
click ¬
menu item appMenuCommand of ¬
menu appMenuName of ¬
menu bar item appMenuName of ¬
menu bar 1 of ¬
application process appName
end clickApplicationMenuCommand
to setURLofFirefoxFrontWindowTo(theURL)
tell application "System Events"
tell application process "Firefox"
set the value of UI element 1 of ¬
combo box 1 of toolbar "Navigation" of ¬
first group of front window to theURL
key code 36 -- # enter key
end tell
end tell
end setURLofFirefoxFrontWindowTo
to waitForFirefoxPageToFinishLoading()
-- # Requires Firefox version 87 or newer.
-- # Requires accessibility.force_disabled set to: -1
tell application "System Events"
tell application process "Firefox"
repeat until exists UI element "Reload" of ¬
toolbar "Navigation" of group 1 of window 1
delay 0.1
end repeat
repeat while (name of UI elements of ¬
toolbar "Navigation" of group 1 of ¬
window 1 whose description is "Reload") ¬
is not {"Reload"}
delay 0.1
end repeat
end tell
end tell
end waitForFirefoxPageToFinishLoading
Notes:
The example AppleScript code, shown above, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Tested using Firefox version 91.0 (64-bit).
Note that UI Scripting is very kludgy and is prone to failure, especially as the version of the OS and or application change, or the value of the delay commands are not sufficient. That said however, with Firefox, this as described herein is what it takes.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.
I use firefox for a long time as my only browser on Pc or Mac.
In a few words my problem: I want to create a service on mac with automator and
Applescript for quite instant translation using translate.google.com.
What works great with Safari or Chrome (below the 4 or 5 lines of script)
On run {input, parameters}
Tell application "Safari"
activate
try
Open location "https://translate.google.com/#auto/en/" & (the clipboard)
end try
end tell
end run
The same thing (script) does not work at all with Firefox, I try by different ways
To circumvent the impossible problem
On run {input, parameters}
Set theProcess to "Firefox"
Set info to {}
Set y to 0
Set x to 0
Set n to 0
Tell application "Applications / Firefox.app"
activate
Open location "http://translate.google.com/#auto/en/"
end tell
Tell application "System events"
Repeat with theProcess in (process "Firefox")
try
Set info to info & (value of (first attribute whose name is "AXWindows") of theProcess)
end try
end repeats
Set n to count of info
info
end tell
Tell application "System Events" to tell process "Firefox"
Set x to "1"
Set frontmost to true
try
Click menu item "Paste" of menu "Edit" of menu bar 1
end try
Repeat while x is "1" -
If x is "1" then
Keystroke "V" using command down
Set x to "0"
end if
end repeat
end tell
end run
By copying and pasting, the actions take place before the complete loading of the page, even by slowing down the Copy and Paste procedure.
After many observations there is a problem of formatting the text contained in the clipboard with the association of the URL, I improved this but it is not perfect yet.
tell application "Applications/Firefox.app" to activate
tell application "System Events" to tell process "Firefox"
set frontmost to true
set sentence to text of (the clipboard)
set thesentences to paragraphs of sentence
set thenewsentences to thesentences as string
set the clipboard to thenewsentences
keystroke "t" using command down
keystroke "https://translate.google.com/#auto/fr/" & (the clipboard) & return
end tell
Anyway if it works with Safari without modifying anything, the problem is at the Firefox entry, so if you can look at this problem, that would be very useful to us all.
Thank you for your attention.
Thank you for your answers.
Safari and Chrome perform the necessary encoding of reserved characters in the URL for you, but Firefox doesn't.
Therefore, you need to perform the encoding of the query-string value (the text to embed in the URL) explicitly.
The easiest (though not obvious) approach is to use perl, via a shell command, gratefully adapted from here:
# Example input that contains chars. that have special meaning in a URL ('&' and '?')
set the clipboard to "Non, Je ne regrette rien & rien ne va plus?"
# Get text from the clipboard and URL-encode it.
set encodedText to do shell script ¬
"perl -MURI::Escape -lne 'print uri_escape($_)' <<<" & quoted form of (the clipboard)
# Now it's safe to append the encoded text to the URL template.
tell application "Firefox"
activate
open location "https://translate.google.com/#auto/en/" & encodedText
end tell
The above approach works with all three browsers mentioned: Firefox, Safari, and Google Chrome.
Note:
As of (at least) Firefox v50, Firefox opens the URL in in a new tab in the current front window by default.
You can make Firefox open the URL in a new window instead, by unchecking Open new windows in a new tab instead on the General tab of Firefox's preferences.
Note, however, that this is a persistent setting that affects all URLs opened from outside of Firefox.
For an ad-hoc solution for opening in a new window that doesn't rely on changing the setting, see this answer of mine.
Hello below the service Automator with some version it is possible to encounter problems, I modified the script so that it works almost everywhere.
A frequent system error is the permission to applications to control your computer that is handled by the system preferences tab Security and Privacy, the system asks if you allow Firefox, TexEdit and others using this service for its keyboard shortcuts.
Also in Automator create service (to be General (and appear in all applications) no entry (up to Yosemite since El Capitan I saw that with Firefox for example all the services are usable), choose Execute a script Applescript paste the script below divided into 2 script or 1 script only.
on run
set Sn to ""
tell application "System Events"
set Sn to the short name of first process whose frontmost is true --here we look for and find which application to launch the service
tell process Sn
set frontmost to true
try
click menu item "Copy" of menu "Edit" of menu bar 1 -- there is used the Copier of the menu Editing of the application
end try
end tell
end tell
return the clipboard
end run
In the script following the entry is done with the contents of the Clipboard
On run {input}
on run {input}
set input to (the clipboard) -- Here we paste the contents of the Clipboard into a variable
try
set input to do shell script "Perl -MURI :: Escape -lne 'print uri_escape ($ _)' <<< " & quoted form of input --To format the text to make it usable with Firefox and the url of translate.google.com
tell application id "org.mozilla.firefox"
activate
open location "https://translate.google.com/#auto/en/" & input --here since version 50 of Firefox you must open a tab and not a new url window of translate.google.com, with option #auto automatic detection by google of the language, and fr to translate into French (these options are modifiable at will)
end tell
end try
end run -- end of the operation you have a tab open on translate, a text to translate and a translated text
I want to open a document in the current tab in an external application (MacVim for example). Based on a StackOverflow answer I created an Automator service with following AppleScript code:
tell application "Xcode"
set current_document to last source document
set current_document_path to path of current_document
end tell
tell application "MacVim"
activate
open current_document_path
end tell
The issue is, that it opens the file from the first tab and not from the current tab. How can I get the path of the current tab?
Following workaround based on SO answer works for me.
As noted in the comment there: This works EXCEPT if you are using the Assistant editor - then you end up with whatever happens to be in the Standard Editor. – Lloyd Sargent but for me it's better than the first tab.
on run {input, parameters}
set current_document_path to ""
tell application "Xcode"
set last_word_in_main_window to (word -1 of (get name of window 1))
if (last_word_in_main_window is "Edited") then
display notification "Please save the current document and try again"
-- eventually we could automatically save the document when this becomes annoying
else
set current_document to document 1 whose name ends with last_word_in_main_window
set current_document_path to path of current_document
end if
end tell
tell application "MacVim"
if (current_document_path is not "") then
activate
open current_document_path
end if
end tell
return input
end run
try the following
tell application "Xcode"
set docs to source documents
if length of docs is less than 1 then
display dialog "for ....... I need at least 1 document"
return
end if
path of last item of source documents
end tell
I'm looking for a way to close specific tabs in safari by the url.
I would probably expand on something like this:
tell application "Safari"
repeat with t in tabs of windows
tell t
if name starts with "google.com" then close
end tell
end repeat
end tell
the problem here is that i haven't found a way how to get the url value of a tab and close them based on that.
You can get the URL from Safari based on the active tab.
tell application "Safari"
set w to first window
set t to current tab of w
display dialog (URL of t as string)
end tell
And then you could iterate over every tab/page like this :
tell application "Safari"
set windowCount to number of windows
repeat with x from 1 to windowCount
set tabCount to number of tabs in window x
repeat with y from 1 to tabCount
set thistab to tab y of window x
set foo to URL of thistab
if foo is not equal to "bar" then close thistab
end repeat
end repeat
end tell
Here is another method:
set closeURLs to {"http://www.yahoo.com", "http://www.apple.com"}
repeat with theURL in closeURLs
tell application "Safari" to close (every tab of every window whose URL contains (contents of theURL))
end repeat
How do you open a new window in safari and then open multiple tabs with different urls in that window using apple script?
The way to create a new window in Safari is to use the make new document command:
make new document at end of documents with properties {URL:the_url}
This will create a new window with a single tab pointing to the_url and make that window frontmost. Note that make new window at end of windows doesn't work, and just errors out with "AppleEvent handler fails".
Similarly, to create a new tab within a window w, you can use make new tab:
make new tab at end of tabs of w with properties {URL:the_url}
This will create a new tab in window w at the end of the list of tabs; this tab will be pointing to the_url, and it won't be the current tab. Instead of explicitly saying tabs of w, you can also use a tell w block:
tell w
make new tab at end of tabs with properties {URL:the_url}
end tell
That way, tabs implicitly refers to tabs of w.
Putting this all together, we get the following script. Given a list of URLs in the_urls, it will open all of them in a new window; if the_urls is empty, it opens a window with a blank tab.
property the_urls : {¬
"http://stackoverflow.com", ¬
"http://tex.stackexchange.com", ¬
"http://apple.stackexchange.com"}
tell application "Safari"
if the_urls = {} then
-- If you don't want to open a new window for an empty list, replace the
-- following line with just "return"
set {first_url, rest_urls} to {"", {}}
else
-- `item 1 of ...` gets the first item of a list, `rest of ...` gets
-- everything after the first item of a list. We treat the two
-- differently because the first item must be placed in a new window, but
-- everything else must be placed in a new tab.
set {first_url, rest_urls} to {item 1 of the_urls, rest of the_urls}
end if
make new document at end of documents with properties {URL:first_url}
tell window 1
repeat with the_url in rest_urls
make new tab at end of tabs with properties {URL:the_url}
end repeat
end tell
end tell
tell application "Safari"
activate
set the URL of document 1 to "http://www.XXXXXXX.com"
my new_tab()
set the URL of document 1 to "http://www.XXXXXX.com"
end tell
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
«event prcsclic» «class menI» "New Tab" of «class menE» "File" of «class mbar» 1
end tell
end tell
end new_tab
Replace the X's with whatever sites you want and keep repeating the code (my new_tab() and set the URL... lines) for each page you'd like to have open.
Referring to this page.
Correct me if this isn't what you were talking about.
Base on Pugmatt's answer I got the following to work...
on run {input, parameters}
tell application "Safari"
activate
make new document with properties {URL:"http://www.apple.com"}
my new_tab()
set the URL of document 1 to "http://www.example.com"
end tell
end run
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
«event prcsclic» «class menI» "New Tab" of «class menE» "File" of «class mbar» 1
end tell
end tell
end new_tab
I'm not sure if this is the most efficient way of you this.