Skip to content

Instantly share code, notes, and snippets.

@firedynasty
Last active January 1, 2023 05:30
Show Gist options
  • Save firedynasty/27d5c8f60a406bf3ce0c27536ff1fd5c to your computer and use it in GitHub Desktop.
Save firedynasty/27d5c8f60a406bf3ce0c27536ff1fd5c to your computer and use it in GitHub Desktop.
batch convert individual images to individual pdf files (applescript)
tell application "Finder"
set all_files to every item of (choose file with prompt "Choose the Files you'd like to rename:" with multiple selections allowed) as list
end tell
set oldDelims to AppleScript's text item delimiters -- save their current state
set text item delimiters to "."
tell application "Finder"
set thefileList to {}
set new_name to "g"
--now we start looping through all selected files. 'index' is our counter that we initially set to 1 and then count up with every file.
--the 'index' number is of course required for the sequential renaming of our files!
repeat with index from 1 to the count of all_files
--using our index, we select the appropriate file from our list
set this_file to item index of all_files
--previously of no
set testpath to POSIX path of this_file
set testname to name of (info for testpath)
set a_ to count testpath
set b_ to count testname
set minus_ to a_ - b_
set oddManOut to text 1 thru (minus_) of testpath
set {itemName, itemExtension} to {name, name extension} of this_file
--if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later
if index is less than 10 then
set index_prefix to "0"
else
set index_prefix to ""
end if
--
--lets check if the current file from our list (based on index-number) has even any file-extension
if itemExtension is "" then
-- "" means there is no file-extension present.
set file_extension to ""
else
--yup, we are currently processing a file that has a file-extension
--we have to re-add the original file-extension after changing the name of the file!
set file_extension to "." & itemExtension
end if
--let's rename our file, add the sequential number from 'index' and add the file-extension to it
--set the name of this_file to new_name & index_prefix & index & file_extension as string
-- set new_name_var to new_name & index_prefix & index & file_extension as string
set new_name_var to testpath as string
--set end of thefileList to oddManOut & new_name_var
set end of thefileList to new_name_var
--put this at the end set end of thefileList to testpath
end repeat
--congratulations for successfully accomplishing the batch renaming task :)
--dont really need set AppleScript's text item delimiters to oldDelims
end tell
set myVar to ""
tell application "Terminal"
activate
do script ""
delay 1
end tell
repeat with a from 1 to length of thefileList
--set theCurrentListItem to item a of thefileList
-- Process the current list item
--set myVar to myVar & item a of thefileList & " "
tell application "Terminal"
activate
set theTab to selected tab in first window
set testpath1 to POSIX path of item a of thefileList
set testname1 to name of (info for testpath1)
set testname2 to text 1 thru -5 of testname1
set testname3 to oddManOut & testname2 & ".pdf"
do script "convert " & quoted form of item a of thefileList & " " & quoted form of testname3 in theTab
end tell
end repeat
return
#some of the code to rename files was copied
#clicking more than 10 files in a row can lead to some error, using less files minimizes error messages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment