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}")