I have an AppleScript that uses VLC to bulk convert .WMV files to .M4V. Because this may take hours to convert a few hundred files, I normally run it in a separate user account on my Mac. I start it running and then switch to another account to do other work. After upgrading to Mojave this script now times out when trying to activate VLC. I suspect this is the result of added security features in Mojave. I have not found a way to correct this problem. For the time being, I’m simply running the script in an active account while I sleep, but I’d prefer to run it as before in an inactive account. Any ideas?
set parent_folders to (choose folder with prompt "Pick the folders containing the files to process:" with multiple selections allowed)
tell application "Finder" to set computer_name to displayed name of computer container
set AppleScript's text item delimiters to ":"
set root_folder to ((text items 1 thru -3 of ((item 1 of parent_folders) as string) as string) & ":")
set archive_folder to root_folder & "wmv files:"
tell application "Finder"
set archive_exists to exists folder archive_folder
if not archive_exists then
make new folder at folder root_folder with properties {name:"wmv files"}
end if
end tell
set change_count to 0
repeat with parent_folder in parent_folders
log parent_folder as string
tell application "VLC" to activate
tell application "System Events" to tell process "VLC"
click menu item "Convert / Stream..." of menu 1 of menu bar item "File" of menu bar 1
delay 1
click button "Open media..." of group 1 of window "Convert & Stream"
tell sheet 1 of window "Convert & Stream"
click pop up button "Where:"
repeat while not (exists UI element 1 of pop up button "Where:")
delay 1
end repeat
click UI element computer_name of UI element 1 of pop up button "Where:"
delay 1
set AppleScript's text item delimiters to ":"
repeat with this_folder in (text items 1 thru -2 of (parent_folder as string))
tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1
repeat with row_number from 2 to count of rows
if this_folder as string = value of text field 1 of UI element 1 of row row_number then
select row row_number
exit repeat
end if
end repeat
end tell
click button "Open"
delay 1
end repeat
end tell
end tell
tell application "Finder" to set these_files to (name of every file of parent_folder)
repeat with this_file in these_files
set start_time to current date
set file_name to this_file as string
if file_name ends with ".wmv" then
tell application "Finder" to set m4v_exists to exists file ((parent_folder as string) & (text 1 thru -4 of file_name) & "m4v")
if not m4v_exists then
tell application "VLC" to activate
tell application "System Events" to tell process "VLC" to click menu item "Convert / Stream..." of menu 1 of menu bar item "File" of menu bar 1
delay 1
tell application "System Events" to tell process "VLC" to tell window "Convert & Stream"
click button "Open media..." of group 1
tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of sheet 1
select row 1
keystroke file_name
end tell
click button "Open" of sheet 1
if exists button "Save as File" of group 3 then
click button "Save as File" of group 3
end if
delay 1
click button "Browse..." of group 3
tell application "VLC" to activate
keystroke text 1 thru -5 of file_name
click button "Save" of sheet 1
click button "Save"
end tell
set new_file_name to (parent_folder as string) & text 1 thru -4 of file_name & "m4v"
set POSIX_file_path to (quoted form of POSIX path of new_file_name)
delay 5
set video_height to do shell script "mdls -name kMDItemPixelHeight " & POSIX_file_path
set loop_count to 0
repeat while video_height ends with "(null)" and loop_count < 50
set loop_count to loop_count + 1
delay loop_count
set video_height to do shell script "mdls -name kMDItemPixelHeight " & POSIX_file_path
end repeat
set video_width to do shell script "mdls -name kMDItemPixelWidth " & POSIX_file_path
set video_duration to do shell script "mdls -name kMDItemDurationSeconds " & POSIX_file_path
set AppleScript's text item delimiters to " = "
set video_height to text item 2 of video_height
set video_width to text item 2 of video_width
if video_duration ends with "(null)" then
set video_duration to 0
else
set video_duration to (text item 2 of video_duration) as integer
end if
set video_duration to video_duration as integer
set duration_minutes to round (video_duration / 60) rounding down
set duration_seconds to video_duration mod 60
if duration_seconds < 10 then
set duration_display to (duration_minutes as string) & ":0" & duration_seconds
else
set duration_display to (duration_minutes as string) & ":" & duration_seconds
end if
set new_comment to (video_width as string) & " X " & video_height & " " & duration_display
tell application "Finder"
set comment of file new_file_name to new_comment
move file file_name in parent_folder to folder archive_folder
end tell
set end_time to current date
set delay_in_tenths to (round ((end_time - start_time) / 6))
set load_time to (delay_in_tenths / 10)
log tab & file_name & tab & " min. " & load_time
set change_count to change_count + 1
end if
end if
end repeat
log (parent_folder as string) & " " & change_count
log ""
end repeat
log "Finished"
return
A terrible script, understandable only by OP … Is it really difficult to break into subroutines?
Your problem is most likely that some window does not have time to appear before you tell to it in the script. Mojave adds security at the expense of more computer resources. Check the appearance of each window on the screen (and maybe buttons):
repeat until SomeWindow exists
delay 0.1
end repeat
Try to conclude some long-time commands in a block with timeout of
Terminate all unneeded processes, remove all unneeded startup login items, turn of all unneeded droplets, workflows, services. Turn off automatically updates, remove malware.
It’s OK on MacScripter to say what’s going wrong with a script with which a poster’s been struggling and has asked for help. It’s also OK to suggest improvements to their coding and style. It’s not OK to make blanket derogatory comments like “terrible script”, especially when you’re a relative newcomer yourself.
I do not intend to offend someone, but only to show that something is wrong and needs to be corrected before asking for help. This is the same as when you yourself write that you need to start the code yourself before asking for help. The word “terrible” is not abusive and MacScripter is not a graveyard where you need to speak quietly.
The code must be clear at least to some extent. I think bad practice is not good for anyone.
I was not offended by either comment, I just ignored them.
I wrote the script for my own use and I do know what it does.
I see no need to break a relatively short piece of code into subroutines that would have just one call.
I’ve been programming since 1963 and I think I’ve learned something about factoring code since I wrote a 45,000 line FORTRAN subroutine full of GOTO’s in 1972.
The line of code that fails is: tell application “VLC” to activate
I’ve never seen an application fail to open in 2 minutes which is the default timeout for AppleScript. However, that could be the problem and I will, as suggested, add an explicit timeout of 10 minutes to see if that solves the problem. I did not think to try that and I appreciate the suggestion. I’ll try that tonight and report the result here.
Part of my problem in addressing this behavior is that it does not occur when the script runs in an active account where I can see what is happening. It only fails when running in an inactive account.
Since this script is the only application running in the inactive account, much of the advice provided seems to me to be irrelevant.
I am intrigued by the suggestion to give priority 1 to the script. I was not aware that such a capability existed. Since this is the only script running it probably doesn’t matter, but if someone could explain this to me I might find it useful in the future.
Tens or hundreds of processes are executed in the background, and the Script Editor process is just one of them. How often a processor deals with a particular process depends on its priority. Priorities have a value of -20 to +20. The highest is -20. But to assign negative priorities you will need administrative permissions. Although, I’m not sure that this will help you. Example:
do shell script "nice -20 '/Applications/Handbrake.app/Contents/MacOS/Handbrake'" with administrator privileges
And you are tried to run only this in separate account ?
delay 10 -- give time to switch
tell application "VLC" to activate
tell application "VLC" to activate
tell application "System Events" to tell process "VLC"
set frontmost to true # ADDED
# File is the menu bar item 3
set ConvertStream_local to name of menu item 13 of menu 1 of menu bar item 3 of menu bar 1 --> "Convertir / Diffuser…" CAUTION, at end is the character ellipsis (…), not the group of 3 dots used in your code.
click menu item ConvertStream_local of menu 1 of menu bar item 3 of menu bar 1
# …
end tell
set windowName_Local to my remplace(ConvertStream_local, "/", "&")
set windowName_Local to my remplace(windowName_Local, "…", "") --> "Convertir & Diffuser" In fact it's "Convertir & diffuser"
tell application "System Events" to tell process "VLC"
tell window windowName_Local
name of button1 of group 1 --> "Ouvrir un média…" CAUTION, at end is the character ellipsis (…), not the group of 3 dots used in your code.
end tell
end tell
#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
set l to text items of t
set AppleScript's text item delimiters to d2
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end remplace
#=====
As you may see, I added some instructions to check that the code works.
I discovered that there seems to be a typo in some strings.
You use :
click menu item “Convert / Stream…” of menu 1 of menu bar item “File” of menu bar 1
click button “Open media…” of group 1
In French the strings don’t end with 3 dots but with the character ellipsis (…)
I didn’t continue because there is no easy way to extract the localized names of UI elements used by VLC.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 31 juillet 2019 18:33:04
Regarding the use of an ellipsis in place of three dots:
The code seems to work with three dots. Perhaps it would also work with an ellipsis. I never saw the need to try that.
The script as written works fine when running in the current active user account on my 2013 Mac Pro. It fails on the first “tell application “VLC” to activate” if I switch user accounts after starting it.
tell application "VLC" to activate
tell application "System Events" to tell process "VLC"
set frontmost to true # ADDED
# File is the menu bar item 3
set ConvertStream_local to name of menu item 13 of menu 1 of menu bar item 3 of menu bar 1 --> "Convertir / Diffuser…" CAUTION, at end is the character ellipsis (…), not the group of 3 dots used in your code.
end tell
set theLast to character -1 of ConvertStream_local --> "…"
As you may see, here it’s the character ellipsis.
Maybe the script will return a single dot on your system.
Honestly I don’t understand (English is not my first language) what you are doing with the user account.
Were is the application installed ?
Here it’s in the global Applications folder:
alias “SSD 500:Applications:”
Maybe on your machine it’s in the folder dedicated to an account.
alias “SSD 500:Users:**********:Applications:”
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 31 juillet 2019 20:04:04
Surprise!!! I just tested my script. It now runs fine in an unattended user account. The only thing that I know has changed is that I’ve upgraded OSX a few time since I last tried this script.