Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

syuksel13

macrumors newbie
Original poster
Jan 17, 2018
3
0
Hello,

I mostly use automator to do most of my job as well as renaming files.
Now what I would like to do is:

1)Sort files using "EXIF" data created date, if no info of EXIF, sort according to file created date (I do not want to use file created date directly, cause EXIF and this are different, first I want to check if it has EXIF data created date, if not using file created date) But all options must be used same time...

This part is critical cause some files has EXIF data, some do not. But I want to sort all of them first checking EXIF created date, and if not using created date.

2)Rename Finder Items: Adding Sequential, according to 1 part
3)Add md5 hash end of name
4)Add year month day using EXIF data created date

At the end, I want file to be seen like this

IMG_000001_df2949e69be243042c9aa6e19e96d615_20171231.jpg


What I cannot do is, I guess I need to use run shellscript in automator, whenever I search google, found that info... But I do not know what to do here.

I need a clear information. I search but all are meaningless to me...

What should I write in Run Shell Script?
I am not expert, just having knowledge of programming basically.
 
Do you want to learn shell scripting or are you asking for someone writing a script a script for you?
Using the shell is fun if you start with simple tasks and continue to learn how to make more complex scripts. It's easier than you might think to get into. Just open Terminal and start with simple commands. One good example of someone who didn't know anything is here on opensource.com. Advanced tutorials are e.g. those from Mike G mikkey or on linuxconfig.org. There are other tutorials around that are more Mac centric, but those should get you started.

I guess most MR members here including me are willing to help if you can present some lines of code that are not working like expected or give you general directions. Asking for a finished solution only works if you're planning to hire someone for the job or if someone is generously enough to hand you out a solution that he already has in his box. Speaking for me, I don't have that solution ready.

Another option is using existing software like SetEXIFData, ExifChanger, ExifRenamer, GraphicConverter or Photo Mechanic and see if it's capable to do your task.

However to get you on track how to get a file renamed to something like IMG_000001_df2949e69be243042c9aa6e19e96d615_20171231.jpg
here are some ideas, that are for sure incomplete and obviously not always the perfect way of how to do shell scripting.

1.) the command that renames or moves a file is mv, read more about it or many other commands in Terminal
Code:
man mv
2.) IMG is a string that you can write directly into your script or put it into a variable like
Code:
myPrefixVariable='IMG'
2.a.) To print a variable type
Code:
echo $myPrefixVariable
2.b.) To use a variable inside a script, just refer to it, like
Code:
$myPrefixVariable
3.) A serial number like 000001 is a variable, too. You need to increment it and preserve the leading zeros, like:
Code:
for ((i=1;i<=5;i++));do printf "%05d\n" $i;done
3.b) You don't just want from 0 to 5?:
Code:
for ((i=1;;i++));do printf "%05d\n" $i;done
That is what you call an infinite loop, exit with CTRL + C. This part isn't exactly what you want for your task, but for demonstrating how to get the output formatted. In the next step, you'll see how to write the printf output of i into another variable. Depending on how you design your final script, this whole for loop construct could be used as a basis to place the rest of the code inside or maybe you won't need the loop at all as it will be done by Automator. If that's the way you go, you only need the part of formatting a variable that is counting up each time a file is getting processed.

4.) To get a md5 checksum:
Code:
md5 /path/to/file
To get /path/to/file written with your real file, just drag and drop your file into Terminal.

4.a.) Put this into a variable for later use:
Code:
myMd5checksum=$(md5 -q /path/to/file)
4.b.) See if it works with:
Code:
echo $myMd5checksum
5.) Your file creation EXIF date is easily grabbed with ExifTool
Code:
exiftool -s3 -DateTimeOriginal -d "%Y%m%d" /path/to/file
5.a.) You want the file creation date if there is no EXIF tag:
Code:
exiftool -s3 -CreateDate -d "%Y%m%d" /path/to/file
This could also be achieved by the GetFileInfo command, but we're using ExifTool anyways. However, make variables from these commands outputs, like in step 4.a.). Then you would need to check if the first of those two variables is empty and if true, use the second variable. You do that with an if statement.

6.) You need to append the .jpg extension:
Code:
myExtension='.jpg'
Notice, that this only works if you're 100% sure, that there are only JPEG files processed. As trivial it may sound, to get the proper file extension for any situation is not a real easy task. More on this here.

7.) Then you need to concatenate the strings, that you're now hopefully able to write from the examples above:
Code:
myFinalFilename=$myPrefixVariable'_'$i'_'$myMd5checksum.......$myExtension
7.a) Finally you rename the file:
Code:
mv /path/to/inputfile/filename /path/to/outputfile/$myFinalFilename

Is that all? No it isn't! You now have all the basic parts of the puzzle, but one of the requirements isn't targeted at all. The serial number you generated in step 3 won't be conform to the order of EXIF date or file creation date. Therefore you'll need to preprocess every file you want to rename in advance, detect the correct order, probably put that into an array, sort it and then take that array as input list for processing your files. That's something I would need to learn by myself, because I'm just a learning user like you and no programmer. So that part remains up to you.

ExifTool is much more powerful and probably you could do the whole task with it alone, but I think it's better to start with some more general scripting for the first time than diving into a much more specialised tool like ExifTool maybe in conjunction with Perl.

You'll have to decide, if you design your script as a pure shell script (probably a bash script) that you save as something like myScript.sh or as a one liner (each command can be separated by a colon as you see above). Then you can call your one liner or bash script directly from Terminal. In that case, you would need to learn more about loops. Or you use Automator or AppleScript for the file handling. In that case, you'll have to learn, how to pass variables from Automator or AppleScript to a shell script. As a first step, try to rename a single file, then loop through a selection of files or folders.

Always work on a copy of your files while fiddling around with unknown commands and have fun with scripting! I hope this helped you a bit.
 
Last edited:
Do you want to learn shell scripting or are you asking for someone writing a script a script for you?
Using the shell is fun if you start with simple tasks and continue to learn how to make more complex scripts. It's easier than you might think to get into. Just open Terminal and start with simple commands. One good example of someone who didn't know anything is here on opensource.com. Advanced tutorials are e.g. those from Mike G mikkey or on linuxconfig.org. There are other tutorials around that are more Mac centric, but those should get you started.

I guess most MR members here including me are willing to help if you can present some lines of code that are not working like expected or give you general directions. Asking for a finished solution only works if you're planning to hire someone for the job or if someone is generously enough to hand you out a solution that he already has in his box. Speaking for me, I don't have that solution ready.

Another option is using existing software like SetEXIFData, ExifChanger, ExifRenamer, GraphicConverter or Photo Mechanic and see if it's capable to do your task.

However to get you on track how to get a file renamed to something like IMG_000001_df2949e69be243042c9aa6e19e96d615_20171231.jpg
here are some ideas, that are for sure incomplete and obviously not always the perfect way of how to do shell scripting.

1.) the command that renames or moves a file is mv, read more about it or many other commands in Terminal
Code:
man mv
2.) IMG is a string that you can write directly into your script or put it into a variable like
Code:
myPrefixVariable='IMG'
2.a.) To print a variable type
Code:
echo $myPrefixVariable
2.b.) To use a variable inside a script, just refer to it, like
Code:
$myPrefixVariable
3.) A serial number like 000001 is a variable, too. You need to increment it and preserve the leading zeros, like:
Code:
for ((i=1;i<=5;i++));do printf "%05d\n" $i;done
3.b) You don't just want from 0 to 5?:
Code:
for ((i=1;;i++));do printf "%05d\n" $i;done
That is what you call an infinite loop, exit with CTRL + C. This part isn't exactly what you want for your task, but for demonstrating how to get the output formatted. In the next step, you'll see how to write the printf output of i into another variable. Depending on how you design your final script, this whole for loop construct could be used as a basis to place the rest of the code inside or maybe you won't need the loop at all as it will be done by Automator. If that's the way you go, you only need the part of formatting a variable that is counting up each time a file is getting processed.

4.) To get a md5 checksum:
Code:
md5 /path/to/file
To get /path/to/file written with your real file, just drag and drop your file into Terminal.

4.a.) Put this into a variable for later use:
Code:
myMd5checksum=$(md5 -q /path/to/file)
4.b.) See if it works with:
Code:
echo $myMd5checksum
5.) Your file creation EXIF date is easily grabbed with ExifTool
Code:
exiftool -s3 -DateTimeOriginal -d "%Y%m%d" /path/to/file
5.a.) You want the file creation date if there is no EXIF tag:
Code:
exiftool -s3 -CreateDate -d "%Y%m%d" /path/to/file
This could also be achieved by the GetFileInfo command, but we're using ExifTool anyways. However, make variables from these commands outputs, like in step 4.a.). Then you would need to check if the first of those two variables is empty and if true, use the second variable. You do that with an if statement.

6.) You need to append the .jpg extension:
Code:
myExtension='.jpg'
Notice, that this only works if you're 100% sure, that there are only JPEG files processed. As trivial it may sound, to get the proper file extension for any situation is not a real easy task. More on this here.

7.) Then you need to concatenate the strings, that you're now hopefully able to write from the examples above:
Code:
myFinalFilename=$myPrefixVariable'_'$i'_'$myMd5checksum.......$myExtension
7.a) Finally you rename the file:
Code:
mv /path/to/inputfile/filename /path/to/outputfile/$myFinalFilename

Is that all? No it isn't! You now have all the basic parts of the puzzle, but one of the requirements isn't targeted at all. The serial number you generated in step 3 won't be conform to the order of EXIF date or file creation date. Therefore you'll need to preprocess every file you want to rename in advance, detect the correct order, probably put that into an array, sort it and then take that array as input list for processing your files. That's something I would need to learn by myself, because I'm just a learning user like you and no programmer. So that part remains up to you.

ExifTool is much more powerful and probably you could do the whole task with it alone, but I think it's better to start with some more general scripting for the first time than diving into a much more specialised tool like ExifTool maybe in conjunction with Perl.

You'll have to decide, if you design your script as a pure shell script (probably a bash script) that you save as something like myScript.sh or as a one liner (each command can be separated by a colon as you see above). Then you can call your one liner or bash script directly from Terminal. In that case, you would need to learn more about loops. Or you use Automator or AppleScript for the file handling. In that case, you'll have to learn, how to pass variables from Automator or AppleScript to a shell script. As a first step, try to rename a single file, then loop through a selection of files or folders.

Always work on a copy of your files while fiddling around with unknown commands and have fun with scripting! I hope this helped you a bit.


Very detailed answer. I will follow and try to learn all of these. First of all, i would like to learn a ready answer. And then these details all you mentioned, I will try to learn... thank you so much.!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.