AppleScript Goal: Preview a file and append text to file name

Goal: Rename files using the existing name of the file, add some text, and append the extension.

I have hundreds of photos in folders on my computer. I’ve synced them up to Dropbox. Since Dropbox doesn’t support MacOS tags, I can only search by filename.

Using Applescript, I thought I could iterate through each file automatically.

  1. I need to see the file (maybe with QLManage -p)
  2. Split the existing file name
  3. Add some ‘free text’ at the end of the file (between brackets)
  4. keep the file extension

Result: ImageName 01-01-2018 13:27:22.jpg
would become
ImageName 01-01-2018 13:27:22 [Tag1 Tag2 Tag3].jpg

My thought on this was that the Applescript would use get selection from finder


tell application "Finder"
	set FileList to selection

It would iterate each item, preview the image:


do shell script "qlmanage -p " & the POSIX path of CurrentItem 

simultaneously prompt for the added text:

 set theResponse to display dialog "The current name is: " & FNDisplay & ". I will add your tags to the end." default answer "" 

The result would then append a [space] and then “[” and “the text” and “]” between the filename, and the extension.

I have a lot of the pieces, I’ve tried to put them together, but I just can’t seem to make it work. I may be thinking too deep on this?

Attempt 1: (I would be ok with a droplet)


on open theDroppedItems
	repeat with a from 1 to length of theDroppedItems
		set theCurrentDroppedItem to item a of theDroppedItems
		
		set Rawfile to the theCurrentDroppedItem
		
		tell application "Finder" to set FNDisplay to name of (theCurrentDroppedItem as alias)
		
		--display dialog FNDisplay
		do shell script "qlmanage -p " & the POSIX path of theCurrentDroppedItem & " > /dev/null 2>&1 &"
		delay 2
		
		activate me
		
		set theResponse to display dialog "The current name is: " & FNDisplay & ". I will add your tags to the end." default answer ""
		
		set theResponse to (text returned of theResponse)
		
		--	display dialog "The current name is: " & FNDisplay & ". The new name will be: " & (name of theCurrentDroppedItem) & theResponse & (name extension of theCurrentDroppedItem)
		
		activate me
		
		set extension hidden of FNDisplay to true
		set thisName to displayed name of FNDisplay
		set extension hidden of FNDisplay to false
		
		tell application "Finder"
			
			set name of file theCurrentDroppedItem to (FNDisplay) & " " & (theResponse) & "." & name extension of theCurrentDroppedItem
			
		end tell
		
		activate me
		
		do shell script "killall qlmanage"
		display dialog "Updated to " & theCurrentDroppedItem & "."
		
		
		
	end repeat
end open


Attempt 2; with Using Finder Selection (I couldn’t get preview to work or the file reconstruction)



tell application "Finder"
	
	set FileList to selection
	
	repeat with oneItem in FileList
		do shell script "qlmanage -p " & (oneItem as alias) & " > /dev/null 2>&1 &"
		delay 2
		
		
		tell (info for oneItem as alias) to set {Nm, Ex} to {name, name extension}
		set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
		BN
		
		
		
		activate me
		
		display dialog BN & " " & Ex
		set Rename to display dialog "Add tags to: " & (name of oneItem as string) default answer ""
		set Rename to (text returned of Rename)
		set Final to BN & " [" & Rename & "]." & Ex
		
		display dialog Final
		
		activate me
		
		do shell script "killall qlmanage"
		
		
	end repeat
end tell

Model: iMac 2012 (late)
AppleScript: 2.7
Browser: Safari 537.36
Operating System: macOS 10.14

ANSWER:



tell application "Finder"
	set FileList to selection
end tell

activate me

repeat with theItem in FileList
	tell application "Finder" to set NameOnly to name of (theItem as alias)
	-- display notification "Now Working on item: " & (theItem as text)
	do shell script "qlmanage -p " & "\"" & the POSIX path of (theItem as text) & "\"" & " > /dev/null 2>&1 &  "
	delay 0.3
	activate me
	
	set openField to display dialog "The current name is: " & NameOnly & ". I will add your tags to the end." default answer ""
	activate me
	
	set theTags to (text returned of openField)
	
	set theTags to " [" & theTags & "]"
	
	set fName to ((items 1 thru ((offset of "." in (get name of theItem)) - 1) of (get name of theItem)) as string) & theTags & "." & name extension of theItem
	
	display notification "You have added: " & theTags & " to the original file: " & NameOnly & " to make:" & fName
	
	tell application "Finder"
		set the name of theItem to fName
	end tell
	
	do shell script "killall qlmanage"
end repeat