Need Help with AppleScript

I have a Tab delimited .txt file with 23 columns and several hundred records. FileName is one of the 23 fields.  I also have a folder of .jpg files that match the filenames in the txt file.


I need to read input .txt file and If the StudentID field is not blank, rename the associated .jpg file with the StudentID.jpg and move it to a Processed output folder.


if StudentID field is blank rename the associated .jpg file in the input folder with Grade_LastName_FirstName.jpg and move the file to the “Errors” output folder. The last thing is to produce two txt files as explained in # 3 and 4 below. These new txt files will be reference files to go with each output folder.  The output folders and txt files can be created on the desktop.


Inputs:

1) Operator selected Folder containing .jpg files - default location desktop

2) Operator selected Tab delimited .txt file with 23 columns - first record is header with field names


Input File has the following fields:


SequenceNumber

FirstName

LastName

StudentID

Teacher

Grade

HomeRoom

Custom1

Custom2

Custom3

Custom4

P1

Q1

P2

Q2

P3

Q3

P4

Q4

Retouch

Spray

FileName

GSBR


Outputs to be created by AppleScript - Script will move renamed files to these two folders:

1) Folder named with name of input folder & “_” & “Processed”  i.e.   If input folder is named Branford, output folder would be Branford_Proceessed 


2) Folder named with name of input folder & “_” & “Errors”  i.e., If input folder is named Branford output folder would be Branford_Errors


3) Tab delimited .txt file with fields FirstName LastName StudentID Grade FileName from the input file for each record that has a StudentID. File should be named with name of input Folder & “_” & “Processed”  i.e.   If input Folder is named Branford, output file would be Branford_Processed.txt


4) Tab delimited .txt file with fields FirstName LastName StudentID Grade FileName from the input file for each record that did not contain a StudentID. File should be named with name of input Folder & “_” & “Errors”  i.e.   If input Folder is named Branford, output file would be Branford_Errors.txt


Any help will be soooo much appreciated.


Posted on Jan 7, 2023 11:07 AM

Reply

Similar questions

46 replies

Jan 12, 2023 8:54 AM in response to Tony T1

Python3 is not installed by the operating system on any release of macOS. Trust me when I say I would rather code in Python (or Ruby) than AppleScript, but I also do not want to force a user to install 3 GB + of Apple Developer tools either, or have to install Python3 from Python.org. Then, the learning curve oversight that goes with it… 🥸

Jan 12, 2023 10:34 AM in response to Tony T1

Success!!! Thanks so much. Looks like I'll be trying to understand Python AND AppleScript.

I did have Python3 on my system. I installed Xcode a while ago out of curiosity.

Can we make two changes.

1. Need the output txt files as tab delimited

2. Name the output files the same as the output folders


I'll test on a larger volume of files but it seems to be working perfectly.

Jan 12, 2023 11:19 AM in response to JB001

I would suggest understanding the newer Shortcuts instead of AppleScript (I really do hate AppleScript).

Python takes a lot of time to learn, but the syntax is such that its easy to see what is happening (you should definitely try to understand the logic in the script I posted)


Writing a tab delimited file is and easy change:

change:
    f1writer = csv.writer(f1)
    f2writer = csv.writer(f2)
to:
    f1writer = csv.writer(f1,delimiter = '\t')
    f2writer = csv.writer(f2, delimiter = '\t')


And name the output files the same as the output folders is also easy:

change:
    errors_txt = os.path.dirname(copy_from_folder) + "/Errors.txt"
    processed_txt = os.path.dirname(copy_from_folder)  + "/Processed.txt"
to:
    errors_txt = os.path.dirname(copy_from_folder) + "/" + os.path.basename(copy_from_folder) + "_errors.txt"
    processed_txt = os.path.dirname(copy_from_folder) + "/" + os.path.basename(copy_from_folder) + "_processed.txt"


Here's the edited script to reflect those changes:


#!/usr/bin/python3

import sys
import os
import csv

if ((len(sys.argv) != 3)):
	exit("usage: " + os.path.basename(sys.argv[0]) + " file_containing_list" + " copy_from_folder" )

file_containing_list = sys.argv[1]
copy_from_folder = sys.argv[2]

ErrorDir = copy_from_folder + "_errors"
if not os.path.exists(ErrorDir):
	os.mkdir(ErrorDir)
ProcessedDir =  copy_from_folder + "_processed"
if not os.path.exists(ProcessedDir):
	os.mkdir(ProcessedDir)
errors_txt = os.path.dirname(copy_from_folder) + "/" + os.path.basename(copy_from_folder) + "_errors.txt"
processed_txt = os.path.dirname(copy_from_folder) + "/" + os.path.basename(copy_from_folder) + "_processed.txt"

with open(file_containing_list) as csvfile:
	reader = csv.DictReader(csvfile, delimiter = '\t')
	f1 = open(errors_txt, 'w')
	f2 = open(processed_txt, 'w')
	f1writer = csv.writer(f1,delimiter = '\t')
	f2writer = csv.writer(f2, delimiter = '\t')
	header = ['FirstName', 'LastName', 'StudentID', 'Grade', 'FileName']
	f1writer.writerow(header)
	f2writer.writerow(header)
	for row in reader:
		if not row['StudentID']:
			newFileName = row['Grade'] + '_' + row['LastName'] + '_' + row['FirstName'] + ".jpg"
			oldFileNamePath = os.path.join(copy_from_folder, row['FileName'])
			newFileNamePath = os.path.join(ErrorDir, newFileName)
			if os.path.exists(oldFileNamePath):
				os.rename(oldFileNamePath, newFileNamePath)
				newrow = [row['FirstName'],row['LastName'],row['StudentID'],row['Grade'],newFileName + ".jpg"]
				f1writer.writerow(newrow)
		elif row['StudentID']:
			newFileName = row['StudentID'] + ".jpg"
			oldFileNamePath = os.path.join(copy_from_folder, row['FileName'])
			newFileNamePath = os.path.join(ProcessedDir, newFileName)
			if os.path.exists(oldFileNamePath):
				os.rename(oldFileNamePath, newFileNamePath)
				newrow = [row['FirstName'],row['LastName'],row['StudentID'],row['Grade'],newFileName + ".jpg"]
				f2writer.writerow(newrow)
	f1.close()
	f2.close()

Jan 12, 2023 11:56 AM in response to Tony T1

I plan to start digging into python.

I've added some comment lines to the code and changed a few fields being written to the text files so they have both the old and new file names. Also fixed the missing .jpg on the processed output folder images. Fairly easy to read - coding is another thing.


As always, this has not only been helpful from a production standpoint but its been a learning experience.

I'm not sure I'll be converting my AppleScripts to Python any time soon. I've created several AppleScripts by using the base code that VikingOSX and others created. They have been real timesavers. And I'll probably be creating more before I can write python. I learn with examples and this is a good start.


Thanks Again.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Need Help with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.