applescript - 如果文件/文件夹路径中有空格,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
解决方案
set myAppFile to (path to me)
set myAppFilePath to POSIX path of ((path to me as text) & "::") --Magic I was looking for
这是我一直在寻找的答案,基本上它的语法需要一些时间来适应,AppleScript 的问题之一......
推荐阅读
- c# - 如何在 uwp 的滚动查看器中获取滚动条高度?
- php - 无法使用 Teams Graph API php 发送 chatMessage
- vue.js - 是否可以将 Nuxt 包导入 VuejS 项目?
- r - R 错误:“check.data 中的错误:参数应该是数字”
- python - 对数组执行数学函数
- excel - 如何对表中的两个字段进行排序,一个名称对应一个日期
- java - 如何在转换时计算任何数据类型的范围
- reactjs - 道具未从 fetch 调用中显示
- neo4j - 在 neo4j-graphql 中,如何在参考当前节点时重用“this”?
- javascript - jquery将类添加到if,else语句中的变量