Mirror Display Script for Self Service

Chrisdahfur
New Contributor II

Hey newbie scripter here. We have a lot of teachers come in and ask our tech team how to mirror the display . So I wrote a small script that will mirror the display for you through Self Service. This will only work if there is an external display connected. It downloads a command line tool created by Fabian Canas and runs it.

( I was trying really hard to make a script myself to mirror the display but finding this command line tool made things way easier.)

#!/bin/bash

## Checking if file exists
if [ -e /Library/Application Support/JAMF/tmp/mirror ]
then

## running the executable 
    /Library/Application Support/JAMF/tmp/mirror
else

## if file doesn't exist
    cd /Library/Application Support/JAMF/tmp/

## it will go ahead and download the file to the tmp directory
    curl -L "https://github.com/fcanas/mirror-displays/releases/download/1.0/MirrorTool.zip" > MirrorTool.zip
    sleep 2
    unzip MirrorTool.zip
    sleep 0.5

## it will then run the downloaded executable
    /Library/Application Support/JAMF/tmp/mirror
fi

exit 0

Definitely looking for any suggestions on how to change this up.

We wanted to store the file under the tmp folder, that can be changed to your taste.

Credit goes to Fabian Canas for making the Mirror Display Application and Command line Tool:
[http://fabiancanas.com/open-source/mirror-displays](link URL)

13 REPLIES 13

joshuasee
Contributor III

If you want to keep such command line utilities for future use, package them with Composer and install/store them in /usr/local/bin . I have several such binaries that I package together and push to all computers when the machines are initially deployed, then invoke them in policies as needed. This would also save bandwidth and protect against the github download being removed, which is nigh inevitable.

Chrisdahfur
New Contributor II

Good point. I do have a package that does this in a similar fashion. The github thing would definitely be an issue in the future tho, Ima rewrite this a bit.

mm2270
Legendary Contributor III

One other thing. I see a lot of people do this in scripts, but it's not necessary to cd into a location, and then do a curl for a file. You can direct curl to download a file direct to a location without first cd'ing into the directory. For example

curl -L "https://github.com/fcanas/mirror-displays/releases/download/1.0/MirrorTool.zip" > "/Library/Application Support/JAMF/tmp/MirrorTool.zip"
sleep 2
unzip "/Library/Application Support/JAMF/tmp/MirrorTool.zip" -d "/Library/Application Support/JAMF/tmp/"
sleep 0.5

It's a minor thing, but just wanted to point it out as it avoids an unnecessary step in the overall sequence. It also means you can download a zip to one location and extract it to some other location, using the -d flag as shown above. Example:

curl -L "https://github.com/fcanas/mirror-displays/releases/download/1.0/MirrorTool.zip" > /private/tmp/MirrorTool.zip
unzip /private/tmp/MirrorTool.zip -d "/Library/Application Support/JAMF/bin/"

Chris_Hafner
Valued Contributor II

I really like the use of Self-Service. This is a great way to get attention there!

That said, part of me wonders if that's the best way to help them. Mirroring displays is a routine, near-daily task for some. It's also very simple. Not only is it the very first selection available from the 'displays menu item' (when connected to a display) it also has a simple shortcut (command+f1).

Anyways, good work!

Chris_Hafner
Valued Contributor II

** I am being reminded that some alternative keyboard layouts may require the use of the (option+command+f1)

Chrisdahfur
New Contributor II

@Chris_Hafner Thanks for checking out the code! Actually I totally know what you mean as it is such a basic task to just go in there and switch it. But since a lot of our professors are old...(no way to put it better) they really don't how to do it and don't really want to learn. Also doesn't help that our buildings are mainly PC houses. So when they have to walk in to to a mac based room they are just confused with looking at a Mac.

Also good point. I was gonna make a basic script where clicking an app would just call the keyboard shortcut and do the mirror swap, but apparently mac doesn't allow the Command button to be inputted into bash/terminal. Sucks.

Chrisdahfur
New Contributor II

@mm2270 Thanks for pointing that out. I need to rewrite it a bit because I want to avoid downloading from the internet and have it locally depend on our servers...in case github decides to delete that mirrortool.zip file.

mm2270
Legendary Contributor III

Yes, totally understood. And I agree that pulling the executable down and packaging it up for deployment is the way to go. Github links sometimes break, projects get moved, etc.

As for keyboard shortcuts and all that, it IS possible to do Command + <something> calls, but it requires AppleScript to do it. And you may run into accessibility problems now that the OS tries to block those kinds of things until they are manually approved. So not really the way to go there unfortunately.

Chrisdahfur
New Contributor II

@mm2270 I totally ran into that problem, when I also tried applecripting this but it would force me to enable it in accessibility. Why apple -.-.

Chris_Hafner
Valued Contributor II

@Corellana I totally understand! Nice work!

dstranathan
Valued Contributor II

If Fabian Canas is out there: Please update w/64-bit (I get a "bad CPU type in executable:" error in Catalina).

mgshepherd
Contributor

@dstranathan I sent him a message through Twitter, let's see what he says.

mgshepherd
Contributor

@dstranathan Be sure you're using 1.2 version. He pointed out I was still using the older 1.02 version. Duh moment for me. lol Good luck!