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 10, 2023 1:14 PM in response to Tony T1

This is where my lack of knowledge really starts to show. I'm not sure how to get the names of what needs to be changed and what to change it to. The dataList has the variable names I need. In the first section of moves I just in the sID field. That is not in the matchList or the errorList being used to move. Those two lists only have fname. So I'm getting very lost.

Jan 10, 2023 1:32 PM in response to JB001

OK, I think I see. You "have a folder of .jpg files that match the filenames in the txt file", so need to match 'something' to something'


This can probably be done as a separate final task in the AppleScript.

Do you have something in a list that can be matched to the files in the "moved folder"?

If you do, then you would be able to loop through the list, look for 'something', then do the rename.

Jan 10, 2023 1:54 PM in response to Tony T1

Yes, each of the moved folders have a list being used to create the two text files. Those lists have a matching file name and the fields needed for the new file name.

The printErrors list is created with firstName {tab} lastName {tab} grade {tab} fname {return}

the fname is a match to the filenames in outFolder1 which need to be changed to grade_firstName_lastName.jpg


How to do that is a mystery to me.


Jan 10, 2023 3:46 PM in response to JB001

I may not have a full enough understanding of the file and text structure you have but...


You can get a list of the filenames in outFolder1 with:

set fList to name of every file in folder "outFolder1" in desktop

And then loop through fList and compare to items in printErrors, and when you get a match, rename the file.


More, see: Mac Automation Scripting Guide: Manipulating Lists of Items



Jan 11, 2023 7:22 AM in response to JB001

What Tony is talking about is a repeat loop and you already use one in your code.

Within the loop you're perform actions on the item.


the Code in Tony's last post will return an AppleScript list of filenames: {"Filename1.jpg", "Filename1.jpg","Filename1.jpg",...}


Repeat with theFile in fList -- with each iteration of the loop, theFile is a variable and is assigned each filename in sequence


set the name of file theFile of folder "outfolder1" to newName

move file theFile to the new location

end repeat


the code above won't work for you as-is.


Hope this helps. B

Jan 11, 2023 9:57 AM in response to bob264

Thanks Bob. I'll see if I can figure the above approach out. I started to take another approach of building a path to the files in the input folder and looping through with a try statement. That's not going much better than using the match list. Right not Script Editor will not open any files so I need to reboot and possibly reinstall it. Nothing is as easy as it seems. Thanks for the suggestions.


Jan 11, 2023 11:23 AM in response to Tony T1

Input File tab delimited text file ->> (myList)


SequenceNumber FirstName LastName StudentID Teacher Grade HomeRoom Custom1 Custom2 Custom3 Custom4 P1 Q1 P2 Q2 P3 Q3 P4 Q4 Retouch Spray FileName GSBR

00001 First1 Last1 1002 Staff AAAAAAAAAA Z 1 SP1_0001.JPG SP2

00002 First2 Last2 11 BBBBBBBBBB D 1 SP1_0002.JPG SP3

00003 First3 Last3 0076 9 CCCCCCCCCC NB 1 SP1_0003.JPG SP2

00004 First4 Last4 0759 Staff DDDDDDDDDD Z 1 IMG_0001.JPG SP2

00005 First5 Last5 7 EEEEEEEEEE NB 1 R IMG_0099.JPG SP2


moveList Returns


{"", "", "Input File tab delimited text file ->> (myList)", "", "SequenceNumber FirstName LastName StudentID Teacher Grade HomeRoom Custom1 Custom2 Custom3 Custom4 P1 Q1 P2 Q2 P3 Q3 P4 Q4 Retouch Spray FileName GSBR

", "00001 First1 Last1 1002 Staff AAAAAAAAAA Z 1 SP1_0001.JPG SP2

", "00002 First2 Last2 11 BBBBBBBBBB D 1 SP1_0002.JPG SP3

", "00003 First3 Last3 0076 9 CCCCCCCCCC NB 1 SP1_0003.JPG SP2

", "00004 First4 Last4 0759 Staff DDDDDDDDDD Z 1 IMG_0001.JPG SP2

", "00005 First5 Last5 7 EEEEEEEEEE NB 1 R IMG_0099.JPG SP2

", "", "", ""}




printErrors

First2 Last2 11 SP1_0002.JPG

First5 Last5 7 IMG_0099.JPG


printProcessed

First1 Last1 1002 Staff SP1_0001.JPG SP2

First3 Last3 0076 9 SP1_0003.JPG SP2

First4 Last4 0759 Staff IMG_0001.JPG SP2



errorList

{"SP1_0002.JPG", "IMG_0099.JPG"}


ProcessedList

{"SP1_0001.JPG", "SP1_0003.JPG", "IMG_0001.JPG"}




Jan 12, 2023 7:33 AM in response to Tony T1

Yes, let's get it working for me first. Then we really need to get it to work with either AppleScript or automator. This will be run by our front desk person who is not very tech savvy.


I never use terminal so I probably messed something up but this is what the terminal window now looks like and nothing ran.


Last login: Wed Jan 11 13:18:23 on console


John@iMac ~ % chmod u+x" /Users/John/Desktop/RenameAndMove.py 


dquote> /Users/John/Desktop/RenameAndMove.py /Users/John/Desktop/04443_ISAAC2299\ -\ Input.txt /Users/John/Desktop/ISAAC 


dquote> 



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

Still doing something wrong. I have an appointment and will get back to this in a couple hours.


this is what terminal looks like now:

John@iMac ~ % 


John@iMac ~ % chmod u+x /Users/John/Desktop/RenameAndMove.py/Users/John/Desktop/RenameAndMove.py /Users/John/Desktop/04443_ISAAC2299\ -\ Input.txt /Users/John/Desktop/ISAAC 

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.