首页 > 解决方案 > 如果文件/文件夹路径中有空格,AppleScript 不起作用

问题描述

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

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

标签: applescript

解决方案


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

这是我一直在寻找的答案,基本上它的语法需要一些时间来适应,AppleScript 的问题之一......


推荐阅读