Skip to content

Instantly share code, notes, and snippets.

@OKsign
Created May 3, 2018 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OKsign/c452adc48c97f120802eef0a4500ceb1 to your computer and use it in GitHub Desktop.
Save OKsign/c452adc48c97f120802eef0a4500ceb1 to your computer and use it in GitHub Desktop.
-- user input
set des1 to "" -- ex. "/Volumes/Data/A/folder"
set ext to {"mkv", "mp4", "avi", "vob"}
-- end of user input
set des1p to POSIX file des1
tell application "Finder"
open des1p
activate
delay 0.3
end tell
tell application "Finder"
set FName to get name of front window
end tell
set AppleScript's text item delimiters to {"/"}
set TarWin to last text item of des1
repeat 300 times
tell application "Finder"
activate
delay 0.2
if FName is TarWin then
exit repeat
end if
end tell
end repeat
tell application "System Events"
keystroke "a" using command down
delay 0.2
end tell
tell application "Finder"
set sel to get selection
try
repeat with i in sel
set FN to get name of i
repeat with x in ext
if FN ends with x then
set mExt to x
exit repeat
end if
end repeat
set CouFN to count FN
set numList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
repeat with N from 1 to CouFN
if character N of FN is "E" then
if character (N + 1) of FN is in numList then
if character (N + 2) of FN is in numList then
set AppleScript's text item delimiters to {""}
set EndName to (characters N thru (N + 2) of FN) as string
set mText to (characters 1 thru (N + 2) of FN) as string
set mTextext to mText & "." & mExt
if character (N - 1) of FN is " " then
reveal i
delay 0.02
set CoumText to count mText
repeat with N from 1 to CoumText
if character N of mText is "S" then
if character (N + 1) of mText is in numList then
if character (N + 2) of mText is in numList then
set FirstP to (characters 1 thru (N + 2) of FN) as string
delay 0.02
set RenameX to FirstP & EndName
set the clipboard to RenameX
delay 0.2
tell application "System Events"
key code 36
delay 0.3
keystroke "v" using command down
delay 0.3
key code 36
delay 1
end tell
end if
end if
end if
end repeat
else
if mTextext is not FN then
reveal i
set the clipboard to mText
delay 0.2
tell application "System Events"
key code 36
delay 0.3
keystroke "v" using command down
delay 0.3
key code 36
delay 1
end tell
end if -- mTextext is not FN
end if -- character (N - 1) of FN is " "
end if
end if
end if
end repeat
end repeat
end try
tell application "System Events"
display notification "Done!"
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment