Macro script doesn't work in Script folder

Hi,
I have written this small script to backup/save files.
If I run it into the macro window it works perfectly.
But run via the UI and script folder it doesn’t create the files/folders (but outputs the prints with the right values and doesn’t trigger any error).
Any idea on why?

#MenuTitle: > Backup current file
# -*- coding: utf-8 -*-

import os
import shutil
from datetime import datetime

# Save current font file
Glyphs.font.save()
print('> Current font file saved')

glyphsFilePath = Glyphs.currentDocument.filePath
glyphsFileName = glyphsFilePath.split('/')[-1]
folderPath = glyphsFilePath.split('/')[1:-1]
folderPath = '/'.join(folderPath)

# Check if Archive folder exists, if not, create it
archivePath = os.path.join(folderPath, 'archive')
if not os.path.exists(archivePath):
    os.makedirs(archivePath)
    print('> Created '+archivePath+' folder')

# Get today's date
todayDate = datetime.today().strftime('%Y-%m-%d')

# Check if a folder with today's date exists in the Archive folder, if not, create it
todayFolderPath = os.path.join(archivePath, todayDate)
if not os.path.exists(todayFolderPath):
    os.makedirs(todayFolderPath)
    print('> Created '+todayFolderPath+' folder')

# Add timestamp to the filename
timestamp = datetime.now().strftime('%H-%M-%S')
newFileName = f"{glyphsFileName.replace('current.glyphs', '')}{timestamp}.glyphs"

# Define the destination path for the .glyphs file
destinationPath = os.path.join(todayFolderPath, newFileName)

# Copy the .glyphs file to the archive/_current_date_folder_
shutil.copy2(glyphsFilePath, destinationPath)
print('> Created '+destinationPath)

message = destinationPath

# Message(message, title='Backup created', OKButton='🫡')

message = 'What about this version?'

comment = AskString(message, value=None, title="New Backup", OKButton='🫡', placeholder='Backup before removing Master Condensed')

markdownFileName = newFileName.replace('.glyphs', '.txt')
markdownPath = os.path.join(todayFolderPath, markdownFileName)

try:
    # Open the file in write mode ('w') and write the text into it
    with open(markdownPath, 'w', encoding='utf-8') as file:
        file.write(comment)
    print(f"Text file '{markdownFileName}' created successfully.")
except Exception as e:
    print(f"An error occurred: {e}")

Do you get an error in the macro window?

Change

folderPath = '/'.join(folderPath)

to

folderPath = '/' + '/'.join(folderPath)

Otherwise the backups are written next to the script and not next to the font file. You can also look into using Pythons pathlib which makes working with paths easier/less error-prone. Or use a proper version control system like Git, since you are already adding messages to your versions.

1 Like

Thanks, it worked!

They were written into the script folder like you said. Good to know!
I tried pathlib first but had the same problem so went back to the old way of doing it.

I’m avoiding git for this, I like the dead simple file in folder approach

Out of curiosity: what is “this” that you avoid git for?

A simple project without the need to collaborate with anyone.

I like having access to the backup through the finder UI.
Git doesn’t bring me anything more than what I already have with the finder.

If there was a simple way to visually preview the glyphs version and restore them selectively (I thought that the now abandonned Glyphs Commit would turn into that) I would reconsider.

In that case you might want to reconsider: Light Table — Formkunft

2 Likes

Just FYI: You totally don’t need to collaborate with anyone in order to use the benefits of git. It is a version control system where you have access to all your former versions and variations in one setup. But again, just fyi.

1 Like

Oh wow! I totally missed that
Thanks for sharing!

For me the biggest issue of git is the lack of visual control.
If given a choice with same features I always prefer GUI and direct file access

But this Light table thing might turn the table! :slight_smile:

Yes, Light Table is amazing. Thanks Florian for making it possible.

1 Like

This plugin is amazing

1 Like