mikeizbicki / cmc-csci046

CMC's Data Structures and Algorithms Course Materials
52 stars 152 forks source link

apple script : open up 2 terminal windows / log into the lambda server / increase font size #487

Open buffeinstein opened 1 year ago

buffeinstein commented 1 year ago

I wrote an Apple Script to open up two terminal windows, log into the lambda server, and increase the font size. I’m running Monterey. I thought I would share my steps in case others want to try:

  1. Open Script Editor on your Mac. You can find it in the Utilities folder within the Applications folder.
  2. Copy and paste your working AppleScript code into a new document.

Here’s the code:

tell application "Terminal"

    activate
    do script "<ssh login>"
    delay 1
    tell application "System Events" to keystroke "<password>"
    delay 1
    tell application "System Events" to keystroke return
    delay 1
    tell application "System Events" to keystroke "clear"
    delay 1
    tell application "System Events" to keystroke return
    delay 1

    activate
    do script "<ssh login>"
    delay 1
    tell application "System Events" to keystroke "<password>"
    delay 1
    tell application "System Events" to keystroke return
    delay 1
    tell application "System Events" to keystroke "clear"
    delay 1
    tell application "System Events" to keystroke return
    delay 1

    set font size of window 1 to 16
    set font size of window 2 to 16

end tell
  1. Click on "File" in the menu bar and choose "Save" or press "Command + S" on your keyboard.
  2. In the "Save As" dialog box, give your script a name and choose "Application" as the file format.
  3. Choose a location to save your script and click the "Save" button.
  4. Go to System Preferences > Security & Privacy > Privacy > Accessibility. Click the lock in the bottom-left corner, and add your application.

I added the app to my desktop so that I can just click it and let it do its thing. Done!

If you’re having trouble with permissions, try running this in your terminal:

$ tccutil reset AppleEvents

I don't know if it's safe to write scripts with your password, but... YOLO?

buffeinstein commented 1 year ago

If you know how to build upon this application, please let me know :)

Here were some things I tried and couldn’t figure out:

Also, sometimes the above code opens up a third terminal, but doesn’t run the commands in it. The other two terminals work as normal.

To edit the application:

  1. Locate the application file in Finder.
  2. Right-click on the application file and select "Show Package Contents" from the context menu.
  3. In the opened folder, navigate to the "Contents" folder and open the "Resources" folder.
  4. Find the file with the extension ".scpt" and double-click to open it in Script Editor.
  5. Make the necessary changes to the script and save the file.
  6. Close the Script Editor and the application file package.
  7. Run the modified application and test if it works as expected.

Note that if you made any changes to the script that require additional permissions, you will need to grant those permissions again when running the modified application.