OS X Automator: Automatically insert text into appearing text prompt - macos

I have purchased Shimo - a VPN client for Mac OS X - and so far am very satisfied with it.
However, I use the service of VPNBook.com which provides free access to some of their VPN servers. The problem is that every 24 hours or so the password to all the free accounts change, the username remains the same.
Shimo offers an option to insert the username for each VPN connection so the only thing that has to be entered is the password.
I have an executable function that returns reads and returns the current password. So, if I run vpnpw into the terminal, it prints the current working password.
The whole thing looks like this:
My plan is now to implement a script that, when Shimo asks for the password, automatically calls vpnpw and inserts it into the prompt. Shimo offers triggers, for example running a shell script or an application when a connection is attempting to be established. So this should make things easier.
Here is a screenshot of how the prompt looks like (sorry for the German language):
What is the best way to do this? I thought about using Automator to run vpnpw whenever the prompt is being displayed. However, I am not experienced with Applescript or Automator in general. Help would be much appreciated because I would like to automate this whole process.
Just for reference, I have attached my script returning the current VPN password for VPNBook.com under this link.

You should be able to do something like this....
on run
set thePwd to do shell script "vpnpw"
set the clipboard to thePwd
end run
then I think you'll have to resort to GUI scripting
tell application "System Events"
keystroke "v" using command down
end tell
You'll need to enable UI scripting in your system preferences before this will work. You can see how/where to do that here
All said and done, your entire script could look something like this...
on run
set thePwd to do shell script "vpnpw"
set the clipboard to thePwd
tell application "Shimo" to activate -- You'll have to check this code, I don't have Shimo to know if this will work
do shell script "sleep 1" -- just to allow time for shimo to become the front app before the paste
tell application "System Events"
keystroke "v" using command down -- paste the clipboard
end tell
end run

Related

Apple Script application control

Thanks for reading my question. I have an issue I was hoping you could help me with. the following apple script will run but ends up timing out because the application is not selected; the icon just bounces on the mac server dock (OSX 10.85). What am I doing wrong?
tell application "(Application Name)"
activate
getURL "(Server URL)"
delay 30
tell database "(database name)"
do script "(Script Name)"
delay 60
close
end tell
end tell
do shell script "(Shell script path)"
Also, I'd like to tell the application to quit prior to running the shell script.
Any and all advice would be appreciated.
THANK YOU!
To quit an app, all you do is put a “quit” command inside its tell block, typically as the last command in the tell block:
tell application "Safari"
quit
end tell
Verify that “getURL” is the right command for the app you are targeting. The modern version is “open location.”
When you run your script from AppleScript Editor, you can tap on “Event Log” at the bottom of the window and see a log of events that occurred during your script’s execution. You can often see what went wrong there.
If your AppleScript is what is timing out, you can use with timeout:
with timeout of 3600 seconds
-- do something within an hour
end timeout
As jweaks said, you need to provide more information to get real help. Every application extends AppleScript in its own way. Your script may be perfect or it may not. There is no way for someone to know without knowing what application your script runs on. It’s like asking for help with Photoshop work but not telling anyone it is Photoshop you are working in.

How to let osascript not build applescript code which will not hit at runtime?

We have a script to send email using Microsoft outlook or Apple mail application. It will dynamically load the default email from system preference (maybe user input also), and using it to decide which mail client to use.
So the code is as following:
if (mailClientStr contains "outlook")
tell application id "com.microsoft.outlook"
-- <<< there will be error if there is no outlook installed
-- <<< even else branch will be run.
...
end tell
else
tell application id "com.apple.mail"
...
end tell
end if
On an machine which doesn't have outlook installed, and the mailClientStr will be "com.apple.mail", but this script cannot be run by osascript
It complains Can’t get application id "com.microsoft.outlook" even the first branch will not be executed. My understanding is osascript will need to access Outlook apple script interface when load and compile this script (before run it).
I can separate the outlook related code into a separate script, but because there is a lot of data to passing, it will be complex, so I don't want this workaround.
So does there any solution from the apple script language side?
From the AppleScript Language Guide:
Entering Script Information in Raw Format
You can enter double angle brackets, or chevrons («»), directly into a script by typing Option-Backslash and Shift-Option-Backslash. You might want to do this if you’re working on a script that needs to use terminology that isn’t available on your current machine—for example, if you’re working at home and don’t have the latest dictionary for a scriptable application you are developing, but you know the codes for a supported term.
You can also use AppleScript to display the underlying codes for a script, using the following steps:
Create a script using standard terms compiled against an available application or scripting addition.
Save the script as text and quit Script Editor.
Remove the application or scripting addition from the computer.
Open the script again and compile it.
When AppleScript asks you to locate the application or scripting addition, cancel the dialog.
Script Editor can compile the script, but displays chevron format for any terms that rely on a missing dictionary

How to circumvent paste-block with typing-script (on a Mac)

I need to RDC into a restricted-use system using Microsoft's Remote Desktop Connection, and I have a Mac. On the Windows system, I am not allowed to copy (reasonable, to protect confidential data) nor to paste, which is not reasonable, as I often would have some piece code from elsewhere that I would love to have on the system.
However, I am obviously allowed to type. My questions is, what is the best practice for such a situation?
My guess: Could I have an AppleScript "typing" everything on the clipboard after using a keyboard shortcut?
Your question is pretty general, hopefully I understood correctly. The following can be done with Applescript.
tell application "System Events"
set temp to the clipboard
set the clipboard to "sometext"
keystroke "v" using command down -- paste
keystroke "some text you want to type"
end tell
The above shows two things : Setting the clipboard value and sending keystrokes. They would both have to be modified for your exact circumstance.
To automatically run the script on a keyboard shortcut you have two options : Use a third party utility like BetterTouchTool and assign the script to a shortcut. Otherwise create a new Automator Service that includes the script and assign it a shortcut in System Preferences.

AppleScript Transmit Script for uploading file to replace itself on web server

So I have this idea for a handy little AppleScript which in my opinion would be very handy in speeding up the process of uploading a local file, to its same location on the server.
In other words, you have to specify the home folder on the server and locally, but once that's finished, it would be nice to just press like "Command" + "Shift" "U" for upload or some other hot key combination not in use by OS X for uploading the currently selected file in the Finder.
I find myself needing to do this a lot, and it will save a lot of time!
Someone please tell me if there is an easier way to do this, but I think this will be a good learning experience on top of it all.
I need some help on how I should get started however..
1) the command line program curl can upload files. 2) if you have a file selected in a Finder window applescript can get the selection. Using those 2 ideas you can automate your task. I don't know the exact curl command but that should be easy to find using google. So you select a file in the Finder and then run the script. The script can be run with a keyboard shortcut as you mentioned or just put it in the Script menu and run it from there.
tell application "Finder"
set selectedFile to item 1 of (get selection)
set selectedFile to selectedFile as text
end tell
do shell script "curl -switchesToUpload " & quoted form of POSIX path of selectedFile
I use Cyberduck which is an ftp client you can set it up so when you double click a file on the server it opens it up in your favorite editor.( textmate is my favorite.) it atuomagiclly downloads and uploads when you save.
this seems like a much better solution to the problem

In OSX can I use a system call to pass a command to a running terminal in a new tab?

Specifically,
In OSX 10.6 from a system call, I want to open a file for editing with VIM in a preexisting terminal (i.e. Terminal.app) by opening a new tab.
Of course I can open a new instance of terminal
/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -e vim MyFile
And, of course I can figure out the PID of the running instance of Terminal but I don't know how to pass a command into that running program. Or, if Terminal supports receiving commands and if it will open a new tab.
If someone knows how to do this with a similar system (e.g. linux and xterm) it could help me figure it out with OSX and Terminal - or, is there some other technique to prevent opening so many terminals instances?
EDIT: CHEAP SOLUTION
I created an AppleAcript script
on run app_arg
tell application "System Events"
tell application process "Terminal"
key code {55, 36}
set frontmost to true
key code {55, 17}
keystroke item 1 of app_arg
keystroke return
end tell
end tell
end run
and run it via the system call like so
/usr/bin/osascript NEWSCRIPT.scpt "args"
It's dirty but it gets the job done - thanks!
The way to accomplish the is with applescript. You can send applescript to things in OS X with the osascript command. I wasn't able to find anything quickly that directly shows how to open a new tab with a command running in it, but I was able to find a couple of references to automating Terminal.app in various other ways with applescript, and I think they may point you in the right direction.
Various random Terminal.app applescript hacks mostly centered around changing colors.
An applescript hack that opens a new terminal window without creating a new Terminal process.
A nice StackOverflow question about how to query an application to discover what applescript it supports (stolen from a comment on your question by #dmckee)
And this nice StackOverflow question concerning Terminal.app's applescript specifically (again stolen from a comment by #dmckee)
An even better exploration of the Leopard Terminal.app's applescript from Ruby no less
And from that last link, it looks like the only way to do it is to use applescript to send the Command-T keystroke to the terminal. That's ugly, but it'll work. And then you can send the command you want to execute. :-)
There are three ways to do this:
Use popen
Use system
Use exec family

Resources