获取 applescript 文件的父目录名称

Get the name of the parent directory of an applescript file

我正在制作一个小脚本,需要使用脚本本身(.scpt 文件)的父目录名称作为变量。

例如脚本位于/Users/spongebob/MyProject/myscript.scpt

我需要将名为 myprojectdir 的变量设置为 MyProject。

我试过了

set myprojectdir to parent of POSIX path of me

以及基于搜索结果的其他变体,但我总是以错误告终

Can’t get POSIX path.

我哪里错了? 谢谢

AppleScript 本身不知道。

你得问System Events

tell application "System Events" to set myprojectdir to name of container of (path to me)

Finder

tell application "Finder" to set myprojectdir to name of parent of (path to me)