【问题标题】:AppleScript doesn't work if Spaces in File/Folder path如果文件/文件夹路径中有空格,AppleScript 不起作用
【发布时间】:2020-04-28 16:56:59
【问题描述】:

这没问题,除非文件夹路径中的任何地方都有空格(可能还有其他特殊字符)。如果有空格,则检查文件夹是否存在失败,如何使其工作?

tell application "Finder"
    set mySaveASDelimiters to AppleScript's text item delimiters

    set myScriptFilePath to (path to me)
    set mySourceFolder to folder of myScriptFilePath
    set myFileAliasList to the entire contents of mySourceFolder

    set myPOSIXSourceFolder to URL of mySourceFolder
    set myPOSIXSourceFolder to characters 8 thru -1 of myPOSIXSourceFolder as string

    repeat with myFile in myFileAliasList
        set myFileName to name of myFile
        log "myFileName: " & myFileName

        if (myFileName ends with ".txt") then
            set AppleScript's text item delimiters to "."
            set myFolderName to first text item of myFileName
            set AppleScript's text item delimiters to mySaveASDelimiters


            set myFolderPath to myPOSIXSourceFolder & myFolderName as string
            log "myFolderPath: " & myFolderPath

            tell application "System Events"
                if not (exists folder myFolderPath) then
                    beep
                    display dialog "Could not find Folder for File: " & myFileName
                    return {}
                end if
            end tell

        end if
    end repeat
end tell

让这更容易:

set myScriptFilePath to (path to me)
set mySourceFolder to folder of myScriptFilePath
set myPOSIXSourceFolder to POSIX path of mySourceFolder

我在 myPOSIXSourceFolder 上收到错误,为什么!我只想要 POSIX 表示法的当前文件夹路径!我现在已经修好了:

set myAppFile to (path to me)
set myAppFilePath to ((path to me as text) & "::")  --Magic I was looking for
set myAppFolder to POSIX path of (myAppFilePath) as string
set myFilePath to myAppFolder & "ModTest1"

log "myAppFilePath: " & myAppFilePath
log "myAppFile: " & myAppFile
log "myAppFolder: " & myAppFolder
log "myFilePath: " & myFilePath


set thePath to myFilePath

tell application "System Events"
    if exists folder thePath then
        beep
    end if
end tell

【问题讨论】:

  • 网址有空格和特殊字符转义,所以你需要处理它们或使用其他方法,如POSIX path
  • 我不知道你说的脚本在哪里,请举个例子。
  • 您将 myPOSIXSourceFolder 设置为一个 URL,该 URL 具有非 ASCII 字符和空格,替换为“%”和十六进制数字。如果您只需要 POSIX 路径,则可以使用文件或别名的 POSIX path 属性(请参阅 AppleScript Language Guide
  • 那不行,见上文。
  • 您仍在通过剥离前 7 个字符来修改文件夹中每个文件的路径,并且在某些情况下您似乎也在尝试剥离扩展名。您要究竟找到什么?

标签: applescript


【解决方案1】:
set myAppFile to (path to me)
set myAppFilePath to POSIX path of ((path to me as text) & "::")  --Magic I was looking for

这是我一直在寻找的答案,基本上它的语法需要一点习惯,AppleScript 的问题之一......

【讨论】:

  • 这是一个非常奇怪和不寻常的语法,在 Finder 告诉块中也是如此:set myAppFilePath to POSIX path of (container of myAppFile as text)
  • 如果可以的话,我会避免使用 Finder
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 2015-07-18
相关资源
最近更新 更多