iOS Shortcuts: Execute Python scripts (using iSH Shell)

foreword

What iOS shortcuts can do is extremely limited. If the shortcut command can run the Python program, the operable space will instantly become larger. iSH is a free iOS software that emulates a Linux-like command line interpreter. We will run the Python program in iSH, and then get the output of the Python program in the shortcut command.

core logic

We use a Python program that "gets the current date" as a demonstration (in fact, there is an operation of "getting the current date" in the shortcut command itself, so this requirement does not need Python, here is just for the convenience of the demonstration), the core code is as follows.

>>> import time
>>> time.strftime('%Y-%m-%d', time.localtime(time.time()))
'2023-02-06'

The logic of the shortcut command is as follows:

  1. Open iSH.
  2. After iSH starts, it will automatically run the Python program we specified, and automatically run an HTTP server.
  3. The output of the Python program will be saved in a specified file. The shortcut command connects to the above server to obtain the output of the Python program.

So the content of iSH's Python script is:

import time
curr_date = time.strftime('%Y-%m-%d', time.localtime(time.time()))
with open('date.txt', 'w') as f:
    f.write(curr_date)

Configure iSH

Install Python

First search for iSH Shell in the AppStore and install it. Open iSH and enter the following command to install Python.

设备名称:~# apk add python3

Test whether the installation is successful.

设备名称:~# python3 --version
Python 3.8.5

Create a Python script

There are many ways to create scripts in iSH, here are two.

The first is to directly type the code in iSH (students who have never used vim, please learn from Baidu).

设备名称:~# vi get_date.py

Then input the content of the above Python script, save and exit the editing interface.

The second is to write the code in the computer and then pass it to iSH.

Taking the win10 system as an example, I C:\tmpwrote it in the directory get_date.py, then opened cmd, entered ipconfig to check the IP of the computer, and found the IPv4 address in the "Wireless LAN Adapter WLAN" column. The IP address displayed on my computer is 192.168.3.99.

Switch cmd to the directory where the Python script is located (ie C:\tmp), and then enter the following command in cmd.

C:\tmp> python3 -m http.server

Back in iSH, install wget.

设备名称:~# apk add wget

After the installation is complete, you can use wget to download the files in the computer (note that the iOS device must be in the same local area network as the computer).

设备名称:~# wget 192.168.3.99:8000/get_date.py

Configure the startup file

The command line interpreter of iSH is ash. Similar to configuring bash, create .profilethe file first.

设备名称:~# vi .profile

Write the following.

if [ -f ~/.ashrc ]; then
   source ~/.ashrc
fi

Create the file again .ashrc.

设备名称:~# vi .ashrc

Write the following.

python3 get_date.py &
python3 -m http.server &

The ones following each line &are running in the background.

This completes the iSH configuration.

Test effect

Close iSH, and then open it again, we found that files appear in the default directory date.txt.

设备名称:~# ls
date.txt
...

Check out this file.

设备名称:~# cat date.txt
2023-02-06

On the same iOS device, open the Safari browser and enter in the address bar 127.0.0.1:8000/date.txt. If the browser can open the link and display the content, the configuration is successful. At this point, there will be a line of output in iSH.

127.0.0.1 - - [06/Feb/2023 08:42:56] "GET /date.txt HTTP/1.1" 200 -

shortcut command

The logic is already very clear, so go directly to the picture.

Please add a picture description

Guess you like

Origin blog.csdn.net/Qmj2333333/article/details/128910249