需要使用applescript来获取所选文件夹

时间:2018-05-03 10:04:14

标签: video applescript directory duration

我实际上有一个applescript,其中提示要求文件夹来获取该文件夹中视频的总持续时间,但是我想通过右键单击该文件夹并选择我的服务来将此AppleScript作为“服务”工作该文件夹中所有视频的持续时间。在那种情况下,我知道如何从脚本服务。所以请帮助我的朋友们。

-- time the following operation:
--set start_time to (time of (current date)) -- start timing

set this_folder to (choose folder with prompt "Pick the folder containing the video files to process:") as string

set video_ext_list to {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "srt", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"}


tell application "Finder"
	set these_files to ((files of folder this_folder whose name extension is in video_ext_list) as alias list)
end tell

set filesCount to count of these_files

set total_duration_of_files_in_seconds to ""
set files_with_no_duration to 0

repeat with i from 1 to filesCount
	set this_file to (item i of my these_files)
	set this_info to info for this_file
	if visible of this_info is true and alias of this_info is false then
		-- insert actions here for: this_file
		set this_file_duration_in_seconds to do shell script "mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & quoted form of POSIX path of this_file
		if this_file_duration_in_seconds is not 0 then
			set total_duration_of_files_in_seconds to total_duration_of_files_in_seconds + this_file_duration_in_seconds
		else
			set files_with_no_duration to files_with_no_duration + 1
		end if
	end if
end repeat

set total_duration_in_hms to do shell script " echo " & total_duration_of_files_in_seconds & " | awk '{printf \"%03d:%02d:%02d\",$0/3600,$0%3600/60,$0%60}'"

(*
set end_time to (time of (current date)) -- stop timing.
set elapsed_time to end_time - start_time

log "2. elapsed_time is is " & elapsed_time & " seconds."
*)

display dialog "Files processed : " & (count of these_files) & return & "Files without duration : " & files_with_no_duration & return & "Total duration of " & (count of these_files) - files_with_no_duration & " files in h:m:s : " & total_duration_in_hms with title "Length of all videos in a folder" buttons {"OK"} default button 1

1 个答案:

答案 0 :(得分:2)

  • 启动Automator
  • 点击按钮New Document或按⌘N
  • 选择Service,然后点击Choose
  • 在画布上方的区域中拖动操作进行以下设置:enter image description here
  • 在搜索字段中输入 run ap
  • Run AppleScript动作拖到画布
  • 替换整个代码
    on run {input, parameters}
        set sourceFolder to item 1 of input
        set video_ext_list to {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "srt", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"}
    
    
        tell application "Finder"
            set these_files to ((files of sourceFolder whose name extension is in video_ext_list) as alias list)
        end tell
    
        set filesCount to count of these_files
    
        set total_duration_of_files_in_seconds to ""
        set files_with_no_duration to 0
    
        repeat with i from 1 to filesCount
            set this_file to (item i of my these_files)
            set this_info to info for this_file
            if visible of this_info is true and alias of this_info is false then
                -- insert actions here for: this_file
                set this_file_duration_in_seconds to do shell script "mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & quoted form of POSIX path of this_file
                if this_file_duration_in_seconds is not "0" then
                    set total_duration_of_files_in_seconds to total_duration_of_files_in_seconds + this_file_duration_in_seconds
                else
                    set files_with_no_duration to files_with_no_duration + 1
                end if
            end if
        end repeat
    
        set total_duration_in_hms to do shell script " echo " & total_duration_of_files_in_seconds & " | awk '{printf \"%03d:%02d:%02d\",$0/3600,$0%3600/60,$0%60}'"
    
        (*
        set end_time to (time of (current date)) -- stop timing.
        set elapsed_time to end_time - start_time
    
        log "2. elapsed_time is is " & elapsed_time & " seconds."
        *)
    
        display dialog "Files processed : " & (count of these_files) & return & "Files without duration : " & files_with_no_duration & return & "Total duration of " & (count of these_files) - files_with_no_duration & " files in h:m:s : " & total_duration_in_hms with title "Length of all videos in a folder" buttons {"OK"} default button 1
    
    
        return input
    end run
    
  • 保存服务

  • 在Finder中选择一个文件夹并从上下文菜单中运行服务

注意:您必须在

中检查字符串"0"而不是整数0
if this_file_duration_in_seconds is not "0" then

因为shell脚本始终返回字符串,而is not 0将始终评估为true