I am attempting to create a script for iTerm2 that will create a session (by a split) and then enter text into that newly created session. How do I activate the newly created session?
tell application "iTerm"
tell current session of current window
split horizontally with default profile
end tell
-- Here is what I need help with?
set _new_session to <what goes here> of current window
tell _new_session
write text "ls"
end tell
end tell
Of course I figured this out shortly after I posted my question. To get the session you must go through the tabs, which I was not doing. Here is a working example:
tell application "iTerm"
tell current session of current window
split horizontally with default profile
end tell
tell current tab of current window
set _new_session to last item of sessions
end tell
tell _new_session
select
write text "ls"
end tell
end tell
If your final target is to execute scripts on the shell you could also use
set sc to "ping google.com"
do shell script (quoted form of sc)
Related
I've an AppleScript that retrieves window id of an app.
Example following script retrieves the window id of Brave Browser.
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 tell
My goal is it possible to convert the window id to a string and vice-versa.
Why conversion?
I want to save window id in UserDefaults on macOS.
Note: This AppleScript is used in macOS app.
To be honest, it's hard for me to understand why the ID should be kept in defaults. Because theID variable will already store this value throughout the script. And at any time you can close the window using a variable in a later part of the script. I can only assume one thing: you want to open a window locally and then close it from other script executed on a remote machine.
And getting the window ID in Brave Browser as text is easy:
set urls to {"https://google.com"}
tell application "Brave Browser"
set myNewWindow to make new window
repeat with theURL in urls
open location theURL
end repeat
delay 0.3
set theID to (id of myNewWindow) as text --> the ID of window in text form
end tell
-- write theID value to defaults here
Closing the window from other script:
-- first, read theID from defaults here
tell application "Brave Browser"
close window id (theID as integer)
end tell
I have an apple script like this
#!/bin/zsh
tell application "iTerm"
activate
select first window
# Create new tab
tell current window
create tab with default profile
end tell
# Split pane
tell current session of current window
split vertically with default profile
split vertically with default profile
split vertically with default profile
end tell
# Exec commands
tell first session of current tab of current window
write text "aws-vault exec my-role -d 12h --no-session"
write text "start him"
end tell
tell second session of current tab of current window
write text "start her"
end tell
tell third session of current tab of current window
write text "start you"
end tell
tell fourth session of current tab of current window
write text "start me"
end tell
end tell
the problem is the script doesn't wait for me to fill in the mfa information from aws command. I've also tried aws-command; start him but that just exits and doesn't execute start him at all. Anyone run into this before?
I don't think this is really possible, because Apple Script has no way of knowing that the aws command requires mfa information and if you are done typing that information.
But there are 2 very hacky ways in which you could achieve this:
Using delay
This option is probably very unreliable, but it may do the job.
You can use the delay command to make AppleScript wait X seconds until it runs write text "start him". Lets say it takes you around 10 seconds to type out the mfa information, then you would use delay 10. Below is how the code would look like.
# more code above...
tell first session of current tab of current window
write text "aws-vault exec my-role -d 12h --no-session"
delay 10 # <-- change this value to your liking
write text "start him"
end tell
# more code below...
Using display dialog
I personally feel this may be the most reliable option for you. What you can do is have a dialog box open and once you have typed out the mfa information, click "Ok" so that the script resumes. So you'd have something like this:
# more code above...
tell first session of current tab of current window
write text "aws-vault exec my-role -d 12h --no-session"
display dialog "Click Ok once you are done "
write text "start him"
end tell
# more code below...
Just a small warning: I haven't tested the above code as I do not own a macOS computer.
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 am trying to setup a TextWrangler script that automatically sends selected code to Julia. Copying a bit from a similar script that does the job for sending code to R I tried the script
tell application "TextWrangler"
set the_selection to (selection of front window as string)
if (the_selection) is "" then
set the_selection to line (get startLine of selection) of front window as string
end if
end tell
tell application "Julia-0.2.1"
activate
cmd the_selection
end tell
It does not work. Probably because of the line containing "cmd the_selection". Anyone a suggestion on how to fix this?
Well if Julia isn't scriptable as #khagler says, then you can usually use gui scripting. This will work as long as Julia has an edit menu and the keyboard shortcut for paste is cmd-v (like in most applications) and your cursor is placed in the proper location for the paste to work. Good luck.
tell application "TextWrangler"
set the_selection to (selection of front window as string)
if (the_selection) is "" then
set the_selection to line (get startLine of selection) of front window as string
end if
end tell
set the clipboard to the_selection
tell application "Julia-0.2.1" to activate
delay 0.2
tell application "System Events" to keystroke "v" using command down
EDIT: I see in your comments that a new Julia window opens every time the script is run. That might be happening because of the line... tell application "Julia" to activate. You might try this line of code in its place. It's another method to bring Julia frontmost before issuing the keystroke commands.
tell application "System Events" to tell process "Julia-0.2.1" to set frontmost to true
Note that if this doesn't bring the Julia window frontmost then it's because the process name is wrong. Sometimes the process name is different than the application name. If true you'll have to figure out the name of the Julia process which you can do by running this and finding its name. You may have to use "Terminal" if Julia runs in a Terminal window.
tell application "System Events" to return name of processes
Julia is not scriptable, so you won't be able to send your code this way. You could try this:
do shell script "julia -e '" & the_selection & "'"
This is equivalent to typing a command into a Terminal window. Depending on what exactly is in your selection, you might need to save it as a file first and then pass the file path instead. Also, note that if julia is not in your path (as would be the case if you just copied the Julia app to your Applications folder) you'll need to specify the full path to the actual executable within the .app package: /Applications/Julia-0.2.1.app/Contents/Resources/julia/bin/julia.
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