【问题标题】:How to automate file move between OneDrive and applications.com.Excel如何在 OneDrive 和 applications.com.Excel 之间自动移动文件
【发布时间】:2019-11-17 08:22:04
【问题描述】:

我一直在Excel上编写数据库,最近要支持Mac,Office 2016及以后的版本确实不支持内联代码,所以我不得不借用Mac。

我想制作一个“补丁”.app,当在最终用户计算机上运行时,它会将 AppleScripts 放入微软所说的文件中。

我能够检测文件夹是否存在并在需要时创建它们。我可以使用桌面和文档目录在命令上移动文件,但是如果我使用 onedrive 目录(文件从该目录执行)它不起作用。

我尝试了几种不同的代码结构,我是 Windows 用户,所以我正在学习操作系统。

我已经尝试过复制和不替换、复制、在 vs. using files、vs. file or items 和every 中输入目录。

这是我现在的代码,几乎可以获取正在运行的脚本的位置,然后删除应用程序的名称并添加其他脚本的正确目录。

检查文件夹是否存在,如果不存在则创建它们。

然后是指将源中的所有文件移动到目标。

set Source to POSIX path of ((path to me as text) & "::") & "AppleScripts/"
set ParentDestination to ("/Library/Application Scripts/")
set Destination to ("/Library/Application Scripts/com.microsoft.Excel/")
set ParentFound to false
set DestinationFound to false

tell application "System Events"
    if exists folder ParentDestination then 
        set ParentFound to true 
        if exists folder Destination then       
            set DestinationFound to true        
        end if  
    end if
end tell

tell application "Finder"
    if ParentFound = false then 
        make new folder at folder "Library" of startup disk with properties {name:"Application Scripts"}    
    end if
    if DestinationFound = false then    
        make new folder at folder "Application Scripts" of folder "Library" of startup disk with properties {name:"com.microsoft.Excel"}    
    end if
    duplicate (every item in Source) to Destination
end tell

我当前的错误是:Finder got an error: Handler can’t handle objects of this class.

这取决于我尝试的代码变体,但它始终在移动所有文件。

我知道这可能是一件简单的事情,但我什至还没有开始让 Excel 调用代码,所以我需要一些帮助。

【问题讨论】:

    标签: excel vba macos applescript


    【解决方案1】:

    Finder 对文件说明符很敏感;这就是为什么我总是喜欢使用系统事件。但是系统事件没有功能性的“复制”命令,所以...以下内容应该适用于本地副本(无论如何,它在我的机器上可以):

    set Source to POSIX path of ((path to me as text) & "::") & "AppleScripts/"
    set libraryFolder to "~/Library"
    
    tell application "System Events"
        if exists folder "Application Scripts" of folder libraryFolder then
            set AppScriptsFolder to folder "Application Scripts" of folder libraryFolder
        else
            set AppScriptsFolder to make new folder at folder libraryFolder with properties {name:"Application Scripts"}
        end if
        if exists folder "com.microsoft.Excel" of AppScriptsFolder then
            set targetFolder to folder "com.microsoft.Excel" of AppScriptsFolder
        else
            set targetFolder to make new folder at AppScriptsFolder with properties {name:"com.microsoft.Excel"}
        end if
        set targetFolder to (path of targetFolder) as alias
        set Source to (path of folder Source) as alias
    end tell
    
    tell application "Finder"
        duplicate (every item in Source) to targetFolder
    end tell
    

    我真诚地怀疑您是否可以从云目录运行 AppleScript。我的意思是,我从未尝试过自己,而且我无权访问 OneDrive 来测试它,但能够从远程驱动器运行任意代码似乎是一场安全噩梦。我很想知道你是否可以管理它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 2012-10-31
      相关资源
      最近更新 更多