A month ago, JailbreakHum classmate wrote an article "Turn your iOS device into a "remote control" for controlling your Mac." It happened that I wanted to write a similar article at that time. When this title came into view, my heart skipped a beat, and N bold letters flashed thr

2024/06/2810:15:33 hotcomm 1458

A month ago, JailbreakHum wrote an article "Turn your iOS device into a "remote control" for controlling your Mac." It happened that I wanted to write a similar article at that time. When this title came into view, my heart skipped a beat, and N bold letters flashed through my mind: "Damn, what I wanted to write was actually preempted! ".

However, after reading it carefully, I found that although it is a step late, my method still has merits. To put it simply, my method achieves the same effect as JailbreakHum’s article. They both use AppleScript, but I don’t need With Hazel, you can actually control your Mac through your iPhone without any third-party applications.

Principle

If you want to control Mac remotely, the key is to have a background program on Mac that can automatically trigger AppleScript according to the instructions on the phone. However, is it necessary for us to buy some applications (such as Hazel) specifically to control the Mac, and then let these applications stay in the background just to control the Mac? This is a double waste of money and memory.

So in my opinion, the trigger used to trigger AppleScript should meet two conditions:

  1. is an application that we have always put in the background;
  2. is the best free application.

The good news is that there are indeed two applications that fully meet the requirements. They are the two native applications Message and Mail. Most people don’t know that both Message and Mail come with the ability to automatically run AppleScript. Let me introduce them one by one.

controls Mac

Message through Message. The way to enable automatic triggering of AppleScript is to open the "General - AppleScript Handler" menu in the application preferences and select the AppleScript we want to run.

A month ago, JailbreakHum classmate wrote an article

Surprisingly, Apple has prepared several very useful AppleScripts for us, such as iTunes Remote Control for remote control of iTunes. The AppleScripts used to run are stored in /Users/username/Library/Application Scripts/com.apple.iChat/.

A month ago, JailbreakHum classmate wrote an article

Click on these AppleScript files. At the beginning of the file is Apple's disclaimer, followed by the script code. The annotations of the code are very detailed, and even people who are not familiar with AppleScript can roughly understand it (a good textbook for learning AppleScript).

A month ago, JailbreakHum classmate wrote an article

Here is an example of selecting iTunes Remote Control. Let's take a look at the effect of this AppleScript first.

First, make sure that the iMessage account we use on our Mac is different from the account on our phone (you can try what happens if the accounts are the same first).

Then, we just need to send play from the iPhone to the iMessage account on the computer, and iTunes will automatically open and play the music. Not only that, you will also receive an iMessage reply telling you what is playing now. In addition to play, other commands include:

  • pause: pause status: play status
  • next: next song
  • previous: previous song
  • mute: mute
  • unmute: unmute
  • help: show help

However, there are two small problems with this AppleScript .

1. The iMessage account on our Mac is generally the same as that on the iPhone. We cannot let the Mac use a different account for the remote control needs, but please see the code here:

on getCurrentiTunesTrackset theCurrentTrackMessage to "Not playing. "

Because the feedback message (theResponse) is defaulted to "Unknown Command.", that is, once any command that is not preset is received, Apple Script will automatically reply "Unknown Command", and "Unknown Command." itself is not defined. Passed instruction! So if you are smart enough, you should understand what will happen when you send yourself a message.

A month ago, JailbreakHum classmate wrote an article

2. What if your gay friends also read this article and sent you "unmute" and "play" while you were in a meeting...?

Therefore, we hope to activate this AppleScript by "sending text messages to myself" and ensure that only our own iMessage account can control the Mac.

In order to solve these problems, I would like to briefly introduce the code of this AppleScript. The code mainly consists of two parts. The first part is similar to a "controller" (message received). It will extract the command when receiving the message from the mobile phone, and then notify the second part (runiTunesRemoteControl) to use the extracted command to run the Mac on iTunes. After the

operation is completed, the second part will generate a feedback information and send it back to the "controller", and then the "controller" will send the feedback information to the mobile phone.

It is conceivable that if the "controller" can check whether the information comes from us when receiving the information, and then decide whether to notify the second part to operate, and can ensure that "Unknown" will not be sent before sending feedback information to the mobile phone. Command." Send it back and everything will be solved, right?

Therefore our changes will focus on the "controller", which is this code:

on message received theMessage from theBuddy for theChat-- pass the command (theMessage) to the first part, and store the feedback information in theResponse set theResponse to runiTunesRemoteControl(theMessage)--Send theResponsesend theResponse to theChatend message received

to the mobile phone. After change:

on message received theMessage from theBuddy for theChatset serviceBuddy to name of service of theBuddy--Check whether the sender is myself if serviceBuddy = myiMessageAccount thenset theResponse to runiTunesRemoteControl (theMessage)-- Make sure the feedback is not "Unknown Command." if theResponse ≠ "Unknown command." thensend theResponse to theChatend ifend ifend message received

In this way, the problem is solved, of course you can also add other Functionality you want, such as turning off the screen, etc. These changes need to be done in the second part of the code, just by adding a few more "if" conditional statements.

If you have a certain programming foundation, this script can become more powerful. It can directly input the information sent from the mobile phone into Termial on Mac. In this way, the Message application on our mobile phone can become a shell-like existence. , can dynamically execute commands uploaded from the mobile phone.

In other words, our mobile phone can remotely control the Mac to do anything. If any readers are interested, I will post the implementation method when updating.

Controls Mac

through Mail. Compared with using Message, the advantage of Mail is that you can set different rules to trigger different AppleScripts, so we don't have to write all AppleScripts in one file or write an AppleScript that needs to schedule other AppleScripts.

In addition, another advantage is that if you have an Android phone, you may not be able to use iMessage to send messages to yourself, but Mail is completely universal. The setting of

Mail is easier than that of Message, because we can directly set the trigger conditions of AppleScript through Mail instead of using code to judge in AppleScript. This is simpler for people without programming foundation.

Here is an example of adding an AppleScript that can turn off the screen.

1. First create an AppleScript that turns off the screen, open the "Script Editor" in the application, and create a new AppleScript. The command is very simple. Just enter a line of do shell script "pmset displaysleepnow", and then save it.

A month ago, JailbreakHum classmate wrote an article

2. Open the mail application and click "Rules - Add Rule" in the preferences;

A month ago, JailbreakHum classmate wrote an article

3. Here we can set the conditions. We set the conditions to "Subject contains Turn off the screen" and "Sender equals XX@ XXX.com (your email)", and select "Run AppleScript" in "Just do the following", and then choose to run the script we wrote in advance that can turn off the screen. After running, delete the email as appropriate.

A month ago, JailbreakHum classmate wrote an article

and Launch Center Pro Workflow integrate

. It is said that there is no laziest, only lazier. Through LCP and Workflow, we do not need to repeatedly enter commands during remote control.

first introduces how to set up the Message version:

1. Create an action in the Workflow, enter the command in the Text block, take "Pause" as an example, and then add a Send Message block.

A month ago, JailbreakHum classmate wrote an article

2. Click the gear in the upper right corner, select "Share Workflow", and select "Add to Launch Center" in the second line.

A month ago, JailbreakHum classmate wrote an article

3. Then in LCP, create a Remote Control group and put Workflow into it.

A month ago, JailbreakHum classmate wrote an article

In this way, we can pause the music on Mac with two clicks (because sending a message requires one click).

For the Mail version, the operation can be completely one-click, because Workflow can send emails within the application. We can set up Workflow like this:

A month ago, JailbreakHum classmate wrote an article

Similarly, after integrating with LCP:

A month ago, JailbreakHum classmate wrote an article

is written at the end

. I originally wanted to introduce how to use "Command - C" to control the remote control, but because of the new feature of macOs Sierra - the universal clipboard, "Command - C" can be said to have withdrawn from the stage of history.

At the same time, I would also like to thank JailbreakHum for his article. Readers may be able to learn more based on his article.

hopes that everyone can learn some useful and fun skills in this article.

Reference article:

  1. "Turn your iOS device into a "remote control" for controlling your Mac"
  2. AutoForwardIMessageText.applescript
  3. ASLR_intro

hotcomm Category Latest News