Как обойти AppleScript <> команда работает из редактора скриптов, но не работает при сохранении в виде .app? - PullRequest
0 голосов
/ 26 августа 2018

Следующий скрипт работает, как требуется, при запуске в редакторе AppleScript, но не при экспорте в .app и запуске напрямую.

tell application "Finder"
    set fld to choose folder with prompt "Choose a volume" default location ("/Volumes")
    set n to name of fld
    set f to format of fld
    display dialog n & " is formatted as " & f
end tell

(*
When run from Script Editor result is:
   Macbook HD is formatted as Mac OS Extended format

When run from a compiled app result is:
   Macbook HD is formatted as «constant ****dfh+»
*)

Я работаю в OS X 10.11.6 (15G22010).Исправления или обходные пути приветствуются.

1 Ответ

0 голосов
/ 26 августа 2018

Проблема в том, что Mac OS Extended format является перечисляемой константой (фактически в целочисленном значении).К нему нельзя получить доступ за пределами блока Finder, и в приложении строка display dialog, по-видимому, рассматривается как находящаяся вне блока телл.Вы должны привести перечисление к тексту.

Я предлагаю этот код, он использует disk класс System Events и перечисляет диски

set theVolumes to list folder "/Volumes"
set chosenVolume to choose from list theVolumes with prompt "Select a volume"
if chosenVolume is false then return
set chosenVolume to item 1 of chosenVolume

tell application "System Events" to set volumeFormat to (format of disk chosenVolume as text)
display dialog chosenVolume & " is formatted as " & volumeFormat

Редактировать: Приведенный выше код явно не работает из-за ошибки.

Это альтернатива с небольшой помощью AppleScriptObjC ифундамент основы

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

use framework "Foundation"

set theVolumes to list folder "/Volumes"
set chosenVolume to choose from list theVolumes with prompt "Select a volume"
if chosenVolume is false then return
set chosenVolume to item 1 of chosenVolume

tell application "System Events" to set volumeURL to URL of disk chosenVolume
set theURL to current application's NSURL's alloc()'s initWithString:volumeURL
set {success, theFormat, theError} to theURL's getResourceValue:(reference) forKey:(current application's NSURLVolumeLocalizedFormatDescriptionKey) |error|:(reference)
if success then
    display dialog chosenVolume & " is formatted as " & (theFormat as text)
else
    display dialog theError's localizedDescription() as text
end if
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...