【问题标题】:Moving a file using AppleScript not working after changing the file name更改文件名后使用 AppleScript 移动文件不起作用
【发布时间】:2019-10-21 14:23:40
【问题描述】:

我正在使用 AppleScript 重命名文件并将其移至文件夹。这是使用语音命令执行的。它不会将文件移动到文件夹中,而是按 Enter,将文件重命名为“myFile”,然后再次按 Enter。

但是,如果我再次执行此操作,或者文件名为“myFile”,它将起作用。我认为移动文件的代码不知道,或者文件名没有更新。我不知道如何解决这个问题。 AppleScript 不是我的菜。

tell application "System Events"
    key code 36
    keystroke "myFile"
    key code 36
end tell

tell application "Finder"
    move POSIX file "/Users/joe/Desktop/myFile.csv" to POSIX file "/Users/joe/Desktop/TestFolder" with replacing
end tell

【问题讨论】:

  • 这可能适用于在 Finder 中选择的内容吗?如果是这样,是否有特殊原因需要编写 UI 脚本而不是仅仅告诉 Finder 或系统事件进行移动和重命名?
  • 我没有 AppleScript 方面的经验。我不确定你在问我什么。

标签: applescript


【解决方案1】:

选择文件后,试试这个:

tell application "Finder"

    set itemlist to the selection
    set theFile to (item 1 of itemlist) as alias
    set name of theFile to "myFile.csv"

    move POSIX file "/Users/joe/Desktop/myFile.csv" to POSIX file "/Users/joe/Desktop/TestFolder" with replacing

end tell

【讨论】:

    【解决方案2】:

    如果当前在 Finder 中选择了您要移动的文件,并且您希望能够设置新名称……此解决方案可能适合您

    property moveToFolder : (path to desktop as text) & "TestFolder"
    
    set newName to text returned of (display dialog "Name Your File" default answer ¬
        "myFile.csv" buttons {"Cancel", "OK"} ¬
        default button 2 cancel button 1 with title "Name Your File")
    
    tell application "Finder"
        set originalFile to item 1 of (get selection) as alias
        set theFile to (move originalFile to alias moveToFolder) as alias
        if (exists of alias (moveToFolder & ":" & newName)) then ¬
            delete alias (moveToFolder & ":" & newName)
        set name of theFile to newName
    end tell
    

    【讨论】:

      【解决方案3】:

      不要使用可以通过 AppleScript 本地命令的 GUI 脚本操作。

      使用 System Events 重命名和移动文件:

      tell application id "sevs"
          set base_folder to folder "~/Desktop/"
          set f_old to "myOldFile.csv" -- file to move (original name)
          set f_new to "myFile.csv"    -- new file name
          set dir to "TestFolder"      -- destination folder
      
          tell the base_folder 
              set the name of the file named f_old to f_new
              move the file named f_new to the folder named dir
          end tell
      end tell
      

      使用 Finder 重命名和移动文件:

      tell application id "MACS" --OR: "com.apple.Finder"
          set base_folder to folder (POSIX file "/Users/joe/Desktop/")
          set f_old to "myOldFile.csv" -- file to move (original name)
          set f_new to "myFile.csv"    -- new file name
          set dir to "TestFolder"      -- destination folder
      
          tell the base_folder 
              set the name of the file named f_old to f_new
              move the file named f_new to the folder named dir with replacing
          end tell
      end tell
      

      基本上,像我一样分解路径后,两个脚本都是相同的,除了分配给变量base_folder 的值,Finder 需要明确知道它是处理 posix 表示法的文件路径。或者,在基本文件夹是桌面文件夹的特定情况下,FinderSystem Events 都应该能够理解这一点:

      set base_folder to the path to the desktop folder
      

      【讨论】:

      • 我目前没有电脑,所以如果上面的脚本有任何小错误,请提前道歉,但是当我有我的电脑回来时,我会回来审查。
      • Finder 甚至可以理解set base_folder to desktop。桌面文件夹是 Finder 的 root 文件夹。
      • @vadian,是的,确实如此。该段的主题是首先指出两个脚本中不同的一行,从而使它们相对于彼此保持独特;然后提供一个替代方案,将两个脚本统一为各自tell块内的相同代码行。但是,您刚刚提醒我desktop folder 不是Finder 中的folder,所以这行不通;而desktop 中的System Events 不是folder。但是,我相信两者都适用于 path to,当然,它返回一个 alias,所以这正是我的目标。
      • 感谢您的回复!您的第一个示例更改了名称,但没有将文件移动到文件夹中。第二个例子没有为我做任何事情。我应该提到一些重要的事情。文件名并不总是相同的。这就是我改变它的原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多