如何在Finder中选择“触摸”(更新日期)文件?

时间:2014-03-10 03:43:52

标签: applescript finder

我做的很多事情是准备用于发布到社交媒体的图片,我有时想通过Applescript“触摸”一些图像(将修改日期更改为现在)。如果我想将文件拖到Applescript应用程序,下面的脚本工作正常,但我真正想要的是一个脚本“触摸当前在finder中选择的任何内容”,而不是在“on open”循环中运行。 (这是我可以随时从Keyboard Maestro运行它,这是自Multifinder以来Mac用户的最佳工具。)

每次尝试进行简单的集合选择,循环选择等都会因某种原因导致错误,因为查找程序无法获取选择中的文件信息或其他内容。任何人都可以提出建议吗?

原始剧本

on open these_items
    repeat with i from 1 to the count of these_items
        set this_item to (item i of these_items)
        set the item_info to info for this_item
        if folder of the item_info is true then
            process_folder(this_item)
        else
            process_item(this_item)
        end if
    end repeat
end open

-- this sub-routine processes folders 
on process_folder(this_folder)
    do shell script "touch \"" & POSIX path of this_folder & "\""
    set these_items to list folder this_folder without invisibles
    repeat with i from 1 to the count of these_items
        set this_item to alias ((this_folder as text) & ¬
            (item i of these_items))
        set the item_info to info for this_item
        if folder of the item_info is true then
            process_folder(this_item)
        else
            process_item(this_item)
        end if
    end repeat
end process_folder

-- this sub-routine processes files 
on process_item(this_item)
    do shell script "touch \"" & POSIX path of this_item & "\""
end process_item
-- Rob

2 个答案:

答案 0 :(得分:1)

这是一个简单的例程,触及在Finder中选择的所有项目。

on touchMe()
    tell application "Finder"
        set myList to the selection
        repeat with myFile in myList
            set myTempItem to myFile as alias
            set myPosixItem to POSIX path of myTempItem
            set myShell to "touch \"" & myTempItem & "\""
            set result to (do shell script myShell with administrator privileges)
            return result
        end repeat
    end tell
end touchMe

编辑:添加了管理员权限。

答案 1 :(得分:0)

感谢大家的帮助。我使用这样的脚本让它工作:

 tell application "Finder"
    set mySelection to selection
    set mySelection2 to selection as text
    tell me
        set mySelection2 to replace_chars(mySelection2, "Mac HD", "")
        set mySelection2 to replace_chars(mySelection2, ":", "/")
        set mySelection2 to replace_chars(mySelection2, " ", "\\ ")
    end tell
    do shell script "touch " & mySelection2 user name "userid" password "password" with administrator privileges

 end tell

 on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
 end replace_chars