Goldbug Software

Second Notes for Mac

Second Notes Script for Mac

Trial

Download Second Notes Script.

macOS 10.13 and later, and Apple silicon only

Second Notes Script for Mac

Second Notes Script is the app adding executing Python script function to Second Notes for Mac. Due to App Store restrictions, apps that use Python cannot be sold, so we will distribute it as a free app. Note that you cannot share notes with Second Notes for iOS using iCloud, which is available in the App Store version.

Python Script function of Second Notes for Mac

Second Notes Script can use Python3 installed on the Mac to execute Python Scripts in order to edit Notes.

The script is used to enter a standard character string, or to enter the result of calculation. And also you can enter the result of scraping the Web site by using an external module. The possibilities are endless.


How to use Python Script

It is necessary that the Python3 is installed on your Mac. Python3 should be located at /opt/homebrew/bin/python3 and Python.framework should be located in /opt/homebrew/frameworks/. Those are made sure at startup, then the script menu in the toolbar will be enabled. And you can select a script from the menu. The script runs on the note you are opening.

Before you run python script, you need to add Python Script in the Script folder of the Second Notes folder in your Mac.

You can add a new script by dragging Python file to the Script folder icon, or select the "New Script" in the context menu which is shown by the control-click at the Script folder.

However, that Python Script requires additional commands for Second Notes. Here is the example. When you run the following Script, a date string will be inserted at the cursor position of the selected Note. If a string is selected in the Note, the string will be replaced the data.


def insert_date():
    import datetime
    now = datetime.datetime.now()
    return now.strftime('%Y/%m/%d(%a)')
        


Install Python3.x

Second Notes uses Python 3.x which has to be installed on your Mac.

The Python 3.x has to be installed in the following location.

/opt/homebrew/frameworks/Python.framework
Second Notes makes sure if the framework is exist, then enable the script menu.


There's a lot of guides about it when you search at the Internet, but the following is the one of them. "Installing Python 3 on Mac OS X".


Here is the short note of using Homebrew to install Python 3.x.

  1. Install XCode or the smaller Command Line Tools.
  2. Install Homebrew.
  3. Install Python 3.x by using Homebrew.


You can also install Python3 by downloading the pkg file from Python.org.

In that case, python.framework is

/Library/Frameworks/Python.framework
will be installed.

It is confirmed that Second Notes Script works when Python 3.12 is installed in this location.

After installing Python 3.x, you can install external modules of Python by using pip3 as well. Those modules can be used from Second Notes script.


Interface between Python Script and Second Notes

Second Notes Script uses "snemb" module to interface between Python and Second Notes. Importing the snemb module in your python script, you can handle the text in the Note from your python script.


Example Scripts

Insert Datas example using snemb module

Changing above InsertDates script to a script using snemb module.


import snemb
import datetime
def insert_date():
    range = snemb.selected_range()
    now = datetime.datetime.now()
    newdate = now.strftime('%Y/%m/%d(%a)')
    snemb.replace_string_in_range(range[0], range[1], newdate)
    return ''
        


Transform date format

An another example is transforming date format such like from "2020.11.1" to "2020/11/1".


import re
import datetime
import snemb
def DateFormatter():
regex = re.compile(r'(\d{4})\.(\d+)\.(\d+)')
text = snemb.document()  # retreve the whole text of the note.
lines = text.splitlines(keepends=True)
dst = '';
for line in lines:
    mm = regex.search(line)
    rval = ''
    if mm and (len(mm.groups()) ≤ 2):
        dt = datetime.datetime.strptime(mm.group(0), '%Y.%m.%d')
        new_dt = dt.strftime('%Y/%m/%d')
        rval = regex.sub(new_dt, line)
        dst += rval
    else:
        dst += line
snemb.set_document(dst)  # Replace the note.
return ''
    

snemb module for debugging

Save the following script as "snemb.py" and import the script, you can test your own Python script outside of Second Notes.


src_text = ''

def document():
  return src_text

def set_document(text):
  global src_text
  src_text = text

def document_length():
  return(len(src_text))

def document_lines():
  lines = src_text.splitlines(keepends=True)
  return(len(lines))

def string_at_line(number):
  lines = src_text.splitlines(keepends=True)
  return lines[number]

def string_in_range(location, length):
  return src_text[location:location+length]

def set_selected_range(location, length):
  global src_selected_range
  src_selected_range = (location, length)

def selected_range():
  global src_selected_range
  return src_selected_range

def replace_string_in_range(location, length, string):
  global src_text
  dst = src_text[:location] + string + src_text[location + length:]
  src_text = dst;

def replace_string_at_line(number, string):
  global src_text
  lines = src_text.splitlines(keepends=True)
  lines[number] = string
  src_text = ''.join(lines)
  
def input_dialog(message, info):
  result = input(message + ', ' + info + ':')
  return result

def text_dialog(string):
  print("--- Begin Text Dialog ---")
  print(string)
  print("--- End Text Dialog ---")
    

When you run testing your own script, please add the following "__main__" codes in your script. The "sample.txt" is for emulating a note which your script works with.


#! /usr/bin/env python3
# coding: utf-8
import snemb
import datetime
def insert_date():
    range = snemb.selected_range()
    now = datetime.datetime.now()
    newdate = now.strftime('%Y/%m/%d(%a)')
    snemb.replace_string_in_range(range[0], range[1], newdate)
    return ''

if __name__ == "__main__":
    filename="sample.txt"
    with open(filename) as f:
        src = f.read()
    snemb.set_document(src)
    snemb.set_selected_range(13,9)
    print("--- Call insert_date()")
    rval = insert_date()
    print ("--- Return value = ", rval)
    print ("--- document =")
    print (snemb.document())
    

If you have any questions or suggestions, please send me at goldbugsoft_support@icloud.com .