Applescript 如何";触摸“;在Finder中选择的文件中(更新日期)?

Applescript 如何";触摸“;在Finder中选择的文件中(更新日期)?,applescript,finder,Applescript,Finder,我经常做的一件事是准备图片发布到社交媒体上,有时我想通过Applescript“触摸”一些图片(将修改日期改为现在)。如果我想将文件拖动到Applescript应用程序中,下面的脚本可以正常工作,但我真正想要的是一个脚本,该脚本“接触到finder中当前选择的任何内容”,而不会在“打开”循环中运行。(这样我就可以随时从键盘大师那里运行它,这是自Multifinder以来Mac用户的最佳工具。) 每次尝试进行简单的集合选择、循环选择等都会由于某种原因导致错误,因为查找器无法获取选择中的文件信息或其

我经常做的一件事是准备图片发布到社交媒体上,有时我想通过Applescript“触摸”一些图片(将修改日期改为现在)。如果我想将文件拖动到Applescript应用程序中,下面的脚本可以正常工作,但我真正想要的是一个脚本,该脚本“接触到finder中当前选择的任何内容”,而不会在“打开”循环中运行。(这样我就可以随时从键盘大师那里运行它,这是自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

下面是一个简单的例程,它涉及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

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

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

 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

谢谢你的评论。当我在删除On touchMe()的情况下以普通脚本运行此脚本时,我实际收到了一个错误(Finder收到了一个错误:touch:Mac HD:Users:pp:Desktop:untitle-1.jpg:Permission denied)(因此,让它只运行而不需要调用)。这有什么办法吗?这就像Finder在直接处理文件时对权限很敏感,但如果将文件拖到脚本应用程序上,则可以。您使用的Mac OS X版本是什么?如果我故意让脚本返回一个错误,我会在文件路径中得到斜杠和冒号。这是一个奇怪的区别。您可以使用管理员权限运行相同的脚本。我将更改脚本,您可以再试一次。很抱歉延迟回复。在此之后,我将发布给我带来问题的脚本。这会给出一个错误:告诉应用程序“Finder”将mySelection设置为与mySelection中的anItem一起重复选择将itemName设置为anItem的名称告诉我touchMe(anItem)--只能与图像一起使用end tell end repeat end tell on touchMe()告诉应用程序“Finder”将myList设置为myList中的选择repeat with myFile将myTempItem设置为myFile作为别名将myPosixItem设置为myTempItem的POSIX路径将myShell设置为“touch\”&myTempItem&“\”,将剪贴板设置为myShell将结果设置为(具有管理员权限的shell脚本myShell)返回结果结束重复结束告知结束touchMeSorry,我不知道如何发布一个合适的大格式脚本响应,只有这些注释。基本上,“with admin privileges”似乎需要密码,然后脚本就不会执行相同的功能了。这对我不起作用: