[Pythonista] The story of making an action to copy selected text

I made an action because the custom keyboard of Pythonista has paste but no copy

Pythonista is convenient, isn't it? It's perfect for coding on the go, and it's easy to create a GUI, so it's very convenient for hacking iOS devices.

The custom keyboard is also good and I have almost no complaints, but I decided to use an action instead because I have a paste key but no copy key.

Introduction What is Pythonista?

First, a brief introduction to Pythonista. Pythonista is a paid app for iOS and is a ** Python development environment ** for both iPhone and iPad.

Pythonista on the App Store

It comes with real Python [^ 1], not an online compiler, and works locally.

In addition, the operability of the custom keyboard designed for iOS is sufficiently practical, and stress-free coding can be performed.

pythonista01-compressor.gif

The included dedicated module is also powerful,

--Access to clipboard --Access to location information --Access to iOS camera features --Data transfer from shared sheet --Access to Objective-C classes

And so on, it is full of functions that can be pulled a little. It is a very recommended environment for fun development by using powerful modules.

Currently, due to the enthusiastic request of users, a beta version of Python 3 series Pythonista is currently under development.

Pythonista 3.x Beta

Create an action to extend the editor

As I did in the above video, in Pythonista you can change the selection range smoothly by swiping the keyboard with two fingers, but it was stressful that the Copy command did not come out after setting the range.

So, after studying Pythonista, I made an action to copy the character string of the selected range to the clipboard. Below is the work procedure.

Create copy_text.py in the site-packages directory

The site-package directory is the directory provided by default in Pythonista, and the import path is in it. (It was written in Readme.txt)

IMG_0013.PNG

As you said, let's create copy_text.py there. The function is divided into two methods, get_selected_text and copy_text, considering the purpose of acquiring and modifying the selected character string later. (Since it is a simple code, I will omit the explanation of the script)

copy_text.py



# coding: utf-8

import sys,editor,clipboard,console
reload(sys)
sys.setdefaultencoding('utf-8')

def get_selected_text():
	text = editor.get_text()
	selection = editor.get_selection()
	selected_text = text[selection[0]:selection[1]]
	return selected_text
		
def copy_text():
	if get_selected_text():
		clipboard.set(get_selected_text())
		
if __name__ == "__main__":
	copy_text()	

When you have finished writing the code, select the text on the edit screen of copy_text.py, execute the Run button, and the selected text should be copied to the clipboard.

Create an action

Then register copy_text.py in the action. To register for an action, click the spanner button at the top right of the screen. Please refer to the video of the situation. (The resolution has been cut in half due to file size issues)

pythonista02_herf-compressor.gif

The procedure is as follows.

  1. Press the spanner button
  2. Press Edit
  3. Press the + mark
  4. Specify a custom command name and icon

use

Just select the text you want to copy while editing any file, press the spanner button and execute Copy.

Of course, you don't need to import the copy_text module in the target file.

IMG_0016.PNG

Conclusion

** Pythonista fun! !! ** **

[^ 1]: In Pythonista 2.0, the Python version is 2.7.5.

Recommended Posts

[Pythonista] The story of making an action to copy selected text
The story of making Python an exe
The story of making an immutable mold
The story of trying to reconnect the client
The story of adding MeCab to ubuntu 16.04
The story of pep8 changing to pycodestyle
The story of making a tool to load an image with Python ⇒ save it as another name
The story of making a lie news generator
The story of making a mel icon generator
The story of moving from Pipenv to Poetry
The story of making soracom_exporter (I tried to monitor SORACOM Air with Prometheus)
The story of making a music generation neural network
The story of sys.path.append ()
The road to Pythonista
The story of wanting to buy Ring Fit Adventure
The story of using circleci to build manylinux wheels
The story of introducing jedi (python auto-completion package) to emacs
An introduction to object orientation-let's change the internal state of an object
The story of copying data from S3 to Google's TeamDrive
After all, the story of returning from Linux to Windows
The story of making a question box bot with discord.py
The story of building Zabbix 4.4
[Apache] The story of prefork
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
The story that the version of python 3.7.7 was not adapted to Heroku
The story of not being able to run pygame with pycharm
How to know the internal structure of an object in Python
A story that struggled to handle the Python package of PocketSphinx
Make the theme of Pythonista 3 like Monokai (how to make your own theme)
The story of making a standard driver for db with python.
The story of making a module that skips mail with python
The story of failing to update "calendar.day_abbr" on the admin screen of django
The story of Python and the story of NaN
The story of participating in AtCoder
Get the attributes of an object
The story of the "hole" in the file
The story of remounting the application server
Supplement to the explanation of vscode
The story of writing a program
The story of making a university 100 yen breakfast LINE bot with Python
zoom I tried to quantify the degree of excitement of the story at the meeting
Python Note: When you want to know the attributes of an object
I tried to extract and illustrate the stage of the story using COTOHA
The story of making a sound camera with Touch Designer and ReSpeaker
The story of the escape probability of a random walk on an integer grid
I tried to visualize the text of the novel "Weathering with You" with WordCloud
I want to leave an arbitrary command in the command history of Shell
The story of making a package that speeds up the operation of Juman (Juman ++) & KNP
I tried the common story of using Deep Learning to predict the Nikkei 225
The story of trying to push SSH_AUTH_SOCK obsolete on screen with LD_PRELOAD
An example of the answer to the reference question of the study session. In python.
Be careful of the type when making an image mask with Numpy