如何使用AppleScript获取文件的路径?

问题描述 投票:-1回答:1

我试图使用AppleScript简单地获取计算机上文件的路径,并将其存储在变量中。我在网上看起来不错,但找不到任何东西。请任何人告诉我如何执行此任务?谢谢。

file path applescript filepath
1个回答
0
投票

有几种获取文件引用的方法,最简单的方法是使用choose filechoose folder之类的东西,它们返回一个别名:

set myVariable to (choose folder)

要在Terminal之类的东西中使用POSIX路径,别名应具有POSIX path属性:

set myVariable to POSIX path of (choose folder)

请注意,某些字符(例如空格)与Terminal中的命令具有不同的含义,因此路径应加引号:

set myVariable to quoted form of POSIX path of (choose folder)

这些可以根据需要混合:

set theFolder to (choose folder with prompt "Choose a folder:")

tell application "Terminal"
  activate
  do script "cd " & quoted form of POSIX path of theFolder
end tell

您还可以在脚本编辑器中使用log语句显示表达式结果和变量内容-有关基本概念,请参见Mac Automation Scripting Guide

© www.soinside.com 2019 - 2024. All rights reserved.