Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

タグ: Service Station

macOS 12.4beta1で「GUIなしツールから起動したAppleScriptでUTIを取得できない」バグ?

Posted on 4月 18, 2022 by Takaaki Naganoya

スクリプトメニューやService Stationなど、「メニューやウィンドウを持たないGUIなしツール」がたくさんmacOS環境にありますが、macOS 12.4上ではこれらのAppleScript起動ツールから起動したAppleScriptから、UniformtypeIdentifiers.frameworkを用いたUTIの取得をブロックされているようです。

スクリプトメニューで実行を確認した(途中までで実行がブロックされてしまった)AppleScriptを、スクリプトエディタ上で実行したところ、問題なく実行されます。ちなみに、内容は選択した画像ファイルをPixelmator Proで超解像処理してクリップボードに設定する(コピーする)という「おかわいらしい」内容のものです。

Service Stationは、コンテクストメニューからのAppleScript実行を可能にするAppleScript実行ツールですが、これについてもしかるべきハンドラを書いて同じAppleScriptを実行させるようにしてみたものの、UTIの取得をブロックされるようです。

→ 追試で、Terminal.app上からosascript経由で実行してみたところ、UniformtypeIdentifiers.frameworkの呼び出しに失敗することが判明

明示的にMain threadで実行しないとダメ???>UniformtypeIdentifiers.framework

AppleScript名:🟧選択中の画像を超解像💝処理してコピー.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/03/25
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.8" — Yosemite (10.10) or later
use framework "Foundation"
use framework "UniformtypeIdentifiers"
use scripting additions

property |NSURL| : a reference to current application’s |NSURL|
property NSArray : a reference to current application’s NSArray
property NSPredicate : a reference to current application’s NSPredicate
property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey

set acceptUTI to "public.image"

set aFile to choose file
(*
tell application "Finder"
  set aFile to first item of (selection as alias list)
end tell
*)

–set aUTI to getUTIFromFile(aFile) of me –選択ファイルからUTIを
–display dialog aUTI
–if aUTI is equal to missing value then return

–set uRes to filterUTIList({aUTI}, acceptUTI) of me –選択ファイルのUTIが、受付可能UTIに含まれるかどうかチェック
–if uRes is equal to {} then return –選択したファイルが画像ではなかった

–掃除
tell application "Pixelmator Pro" to close every document without saving

–取得した画像を超解像処理してコピー
tell application "Pixelmator Pro"
  try
    open aFile
  on error
    close every document without saving
    
return
  end try
  
  
tell front document
    with timeout of 3000 seconds
      super resolution
    end timeout
  end tell
end tell

tell application "Pixelmator Pro"
  tell front document
    select all
    
copy
    
close without saving
  end tell
end tell

on getUTIFromFile(aFile)
  set aPath to POSIX path of aFile
  
  
set aWS to current application’s NSWorkspace’s sharedWorkspace()
  
set pRes to (aWS’s isFilePackageAtPath:aPath) as boolean
  
if pRes = false then
    set superType to (current application’s UTTypeData)
  else
    set superType to (current application’s UTTypePackage)
  end if
  
  
set pathString to current application’s NSString’s stringWithString:aPath
  
set aExt to (pathString’s pathExtension()) as string
  
  
set aUTType to current application’s UTType’s typeWithFilenameExtension:aExt conformingToType:(superType)
  
  
set aUTIstr to aUTType’s identifier() as string
  
return aUTIstr
end getUTIFromFile

–UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
  set anArray to NSArray’s arrayWithArray:aUTIList
  
set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
  
set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list
  
return bRes
end filterUTIList

★Click Here to Open This Script 

Posted in Bug news UTI | Tagged 12.0savvy Pixelmator Pro Service Station | Leave a comment

不可視プロセスで表示したNSAlertを最前面に表示

Posted on 1月 28, 2022 by Takaaki Naganoya

コンテクストメニューからAppleScriptを実行するツール「Service Station」を連日こづき回していろいろ試しています。

その中で、Service StationではScript実行をGUIなしの補助プログラムからosascriptコマンドを経由してAppleScriptを実行しているために、AppleScript中でNSAlertによるダイアログ表示をおこなった際に、ダイアログが最前面に表示されないという問題が生じていました。

これを見たedama2さんから、強制的にNSAlertダイアログを最前面に表示させるコードを提供していただいたので、実際にテストに用いたAppleScriptに入れたものをご紹介します。


▲そのまま普通にNSAlertダイアログを表示させたところ。Service Stationによるコンテクストメニューから実行すると、最前面に表示されないという問題があった


▲edama2さんからもたらされたコードを追加したNSAlertダイアログ。Service Stationから実行しても、問題なく最前面に表示される

コードは2行、効き目はばっちり!

--Move to frontmost (By edama2)
current application's NSApplication's sharedApplication()'s setActivationPolicy:(current application's NSApplicationActivationPolicyRegular)
current application's NSApp's activateIgnoringOtherApps:(true)

副作用として、Dockにターミナルのアイコンが表示され、最前面のアプリケーションが「osascript」になってしまいますが、気にならないところでしょう。

Program Name Name of runtime Support AppleScript document format AS Format NSAlert dialog is displayed in frontmost Can use GUI Scripting functions Can call AppleScript Libraries? Can call AS Library including Frameworks? Can call Generic Cocoa Functions? Can use Cocoa system notification functions?
Service Station osascript Script/Text Script/Applet Script/Text Script No-> Yes Yes Yes No Yes No
Script Menu osascript Script/Scriptd/Applet Script/Scriptd Yes Yes Yes No Yes Yes
Shortcuts Events MacHelper File Embedded Script Text Yes Yes Yes No Yes No
Switch Control Assistive Control File Embedded Script Script (archived) Yes Yes No No Yes No
FastScript 3 FastScripts Script Runner Script/Scriptd/Applet Script/Scriptd Yes Yes Yes No Yes

あれ? edama2さんのおかげで、Service Stationの弱点が1つなくなりましたよ。Service Stationについては、

(1)「Kind–> Script」「AppleScript」でテキストで書いた「.applescript」しかピックアップできないとかいう気の狂ったようなRulesの仕様は要・修正

(2)実行Scriptにフラットな.scptとテキストの.applescriptしか実行できないのはダメ。バンドル形式の.scptdの実行サポートは必須

(3)大量のAppleScriptをコンテクストメニューに入れると使い勝手が落ちるので、Rulesでもう少しこまかく条件付けできるとよさそう。ただし必須ではない

といったところでしょうか。

AppleScript名:Webダイアログ表示(強制最前面).scpt
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

my serviceStationDidSelect("", "", "")

on serviceStationDidSelect(targetedURL, selectedItemURLs, menuKind)
  set aList to {{label:"TEST1", value:403}, {label:"TEST2", value:301}, {label:"TEST3", value:101}, {label:"TEST4", value:65}}
  
set bList to sortRecListByLabel(aList, {"value"}, {false}) of me –降順ソート
  
  
–https://www.amcharts.com/demos/pie-chart/
  
set myStr to retHTML() of me
  
set jsonStr to array2DToJSONArray(bList) of me as string
  
set aString to current application’s NSString’s stringWithFormat_(myStr, jsonStr) as string
  
set paramObj to {myMessage:"Simple Pie Chart", mySubMessage:"This is a AMCharts test", htmlStr:aString, viewSize:{1000, 550}}
  
  
my webD’s displayWebDialog(paramObj)
end serviceStationDidSelect

on array2DToJSONArray(aList)
  set anArray to current application’s NSMutableArray’s arrayWithArray:aList
  
set jsonData to current application’s NSJSONSerialization’s dataWithJSONObject:anArray options:(0 as integer) |error|:(missing value)
  
set resString to current application’s NSString’s alloc()’s initWithData:jsonData encoding:(current application’s NSUTF8StringEncoding)
  
return resString
end array2DToJSONArray

–リストに入れたレコードを、指定の属性ラベルの値でソート
on sortRecListByLabel(aRecList as list, aLabelStr as list, ascendF as list)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
  
set aCount to length of aLabelStr
  
set sortDescArray to current application’s NSMutableArray’s new()
  
repeat with i from 1 to aCount
    set aLabel to (item i of aLabelStr)
    
set aKey to (item i of ascendF)
    
set sortDesc to (current application’s NSSortDescriptor’s alloc()’s initWithKey:aLabel ascending:aKey)
    (
sortDescArray’s addObject:sortDesc)
  end repeat
  
  
return (aArray’s sortedArrayUsingDescriptors:sortDescArray) as list
end sortRecListByLabel

script webD
  —
  
–  Created by: Takaaki Naganoya
  
–  Created on: 2020/06/20
  
—
  
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
  
—
  
use AppleScript
  
use framework "Foundation"
  
use framework "AppKit"
  
use framework "WebKit"
  
use scripting additions
  
property parent : AppleScript
  
  
property |NSURL| : a reference to current application’s |NSURL|
  
property NSAlert : a reference to current application’s NSAlert
  
property NSString : a reference to current application’s NSString
  
property NSButton : a reference to current application’s NSButton
  
property WKWebView : a reference to current application’s WKWebView
  
property WKUserScript : a reference to current application’s WKUserScript
  
property NSURLRequest : a reference to current application’s NSURLRequest
  
property NSRunningApplication : a reference to current application’s NSRunningApplication
  
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
  
property WKUserContentController : a reference to current application’s WKUserContentController
  
property WKWebViewConfiguration : a reference to current application’s WKWebViewConfiguration
  
property WKUserScriptInjectionTimeAtDocumentEnd : a reference to current application’s WKUserScriptInjectionTimeAtDocumentEnd
  
  
property returnCode : 0
  
  
  
on displayWebDialog(paramObj)
    my performSelectorOnMainThread:"browseStrWebContents:" withObject:(paramObj) waitUntilDone:true
  end displayWebDialog
  
  
on browseStrWebContents:paramObj
    set aMainMes to myMessage of paramObj as string
    
set aSubMes to mySubMessage of paramObj as string
    
set htmlString to (htmlStr of paramObj) as string
    
–set jsDelimList to (jsDelimiters of paramObj) as list
    
set webSize to (viewSize of paramObj) as list
    
    
copy webSize to {aWidth, aHeight}
    
    
–WebViewをつくる
    
set aConf to WKWebViewConfiguration’s alloc()’s init()
    
    
set aWebView to WKWebView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) configuration:aConf
    
aWebView’s setNavigationDelegate:me
    
aWebView’s setUIDelegate:me
    
aWebView’s setTranslatesAutoresizingMaskIntoConstraints:true
    
using terms from scripting additions
      set bURL to |NSURL|’s fileURLWithPath:(POSIX path of (path to me))
    end using terms from
    
aWebView’s loadHTMLString:htmlString baseURL:(bURL)
    
    
–Move to frontmost (By edama2)
    
current application’s NSApplication’s sharedApplication()’s setActivationPolicy:(current application’s NSApplicationActivationPolicyRegular)
    
current application’s NSApp’s activateIgnoringOtherApps:(true)
    
    
— set up alert  
    
set theAlert to NSAlert’s alloc()’s init()
    
tell theAlert
      its setMessageText:aMainMes
      
its setInformativeText:aSubMes
      
its addButtonWithTitle:"OK"
      
–its addButtonWithTitle:"Cancel"
      
its setAccessoryView:aWebView
      
      
set myWindow to its |window|
    end tell
    
    
— show alert in modal loop
    
NSRunningApplication’s currentApplication()’s activateWithOptions:0
    
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
    
    
if (my returnCode as number) = 1001 then error number -128
    
    
##後始末
    
aWebView’s stopLoading()
    
set js to "window.open(’about:blank’,’_self’).close();"
    
aWebView’s evaluateJavaScript:js completionHandler:(missing value)
    
set aWebView to missing value
    
  end browseStrWebContents:
  
  
  
on doModal:aParam
    set (my returnCode) to (aParam’s runModal()) as number
  end doModal:
  
  
  
on viewDidLoad:aNotification
    return true
  end viewDidLoad:
  
  
  
on fetchJSSourceString(aURL)
    set jsURL to |NSURL|’s URLWithString:aURL
    
set jsSourceString to NSString’s stringWithContentsOfURL:jsURL encoding:(NSUTF8StringEncoding) |error|:(missing value)
    
return jsSourceString
  end fetchJSSourceString
  
  
  
on pickUpFromToStr(aStr as string, s1Str as string, s2Str as string)
    using terms from scripting additions
      set a1Offset to offset of s1Str in aStr
      
if a1Offset = 0 then return false
      
set bStr to text (a1Offset + (length of s1Str)) thru -1 of aStr
      
set a2Offset to offset of s2Str in bStr
      
if a2Offset = 0 then return false
      
set cStr to text 1 thru (a2Offset – (length of s2Str)) of bStr
      
return cStr as string
    end using terms from
  end pickUpFromToStr
  
  
  
–リストを任意のデリミタ付きでテキストに
  
on retArrowText(aList, aDelim)
    using terms from scripting additions
      set aText to ""
      
set curDelim to AppleScript’s text item delimiters
      
set AppleScript’s text item delimiters to aDelim
      
set aText to aList as text
      
set AppleScript’s text item delimiters to curDelim
      
return aText
    end using terms from
  end retArrowText
  
  
on array2DToJSONArray(aList)
    set anArray to current application’s NSMutableArray’s arrayWithArray:aList
    
set jsonData to current application’s NSJSONSerialization’s dataWithJSONObject:anArray options:(0 as integer) |error|:(missing value) –0 is
    
set resString to current application’s NSString’s alloc()’s initWithData:jsonData encoding:(current application’s NSUTF8StringEncoding)
    
return resString
  end array2DToJSONArray
  
  
  
on parseByDelim(aData, aDelim)
    using terms from scripting additions
      set curDelim to AppleScript’s text item delimiters
      
set AppleScript’s text item delimiters to aDelim
      
set dList to text items of aData
      
set AppleScript’s text item delimiters to curDelim
      
return dList
    end using terms from
  end parseByDelim
  
end script

on retHTML()
  return "<!doctype html>
<head>
<meta charset=\"utf-8\">
<!– Styles –>
<style>
body { background-color: #000000; color: #ffffff; }
#chartdiv {
width: 100%;
height: 500px;
}

</style>
</head>
<body>

<!– Resources –>
<script src=\"https://www.amcharts.com/lib/4/core.js\"></script>
<script src=\"https://www.amcharts.com/lib/4/charts.js\"></script>
<script src=\"https://www.amcharts.com/lib/4/themes/dark.js\"></script>
<script src=\"https://www.amcharts.com/lib/4/themes/animated.js\"></script>

<!– Chart code –>
<script>
am4core.ready(function() {

// Themes begin
am4core.useTheme(am4themes_dark);
am4core.useTheme(am4themes_animated);
// Themes end

// Create chart instance
var chart = am4core.create(\"chartdiv\", am4charts.PieChart);

// Add data
chart.data = %@;

// Add and configure Series
var pieSeries = chart.series.push(new am4charts.PieSeries());
pieSeries.dataFields.value = \"value\";
pieSeries.dataFields.category = \"label\";
pieSeries.slices.template.stroke = am4core.color(\"#fff\");
pieSeries.slices.template.strokeOpacity = 1;

// This creates initial animation
pieSeries.hiddenState.properties.opacity = 1;
pieSeries.hiddenState.properties.endAngle = -90;
pieSeries.hiddenState.properties.startAngle = -90;

chart.hiddenState.properties.radius = am4core.percent(0);

}); // end am4core.ready()
</script>

<!– HTML –>
<div id=\"chartdiv\"></div>
</body>
</html>"

end retHTML

★Click Here to Open This Script 

Posted in JSON Review | Tagged 12.0savvy NSAlert NSButton NSRunningApplication NSString NSURL NSURLRequest Service Station WKUserContentController WKUserScript WKUserScriptInjectionTimeAtDocumentEnd WKWebView WKWebViewConfiguration | Leave a comment

Service Stationと他のメニュー系AppleScript実行環境との比較

Posted on 1月 28, 2022 by Takaaki Naganoya

AppleScriptの実行環境のうち、メニューから実行するタイプのものと「Service Station」の比較を行なってみました。

■Service Station


コンテクストメニューからAppleScriptを実行するツール。Finder上で選択中のファイルに応じてコンテクストメニューに表示する内容を変更できる。

■Script Menu(Apple)


macOS標準装備、メニューバーから呼び出し可能。アプリケーションごとに自動メニュー切り替え。指定フォルダ以下の内容を階層化したメニューとして表示する。

■Shorcuts Events(Apple)


macOS 12より標準装備。ショートカットをメニューバーから実行できる。メニューバーに表示するよう指定したものが表示される。階層化メニューはサポートしていない。単なるメニューのくせに、アホみたいに動作が遅い。壊れているのかと思えるほど。iCloudを経由して第三者にアクションを配布できるようになっている点が他の環境にない特徴。ただし、まだバグの塊。

■Switch Control(Apple)

macOS標準装備。アクセシビリティ系の機能。画面上に自由に配置できるフローティングパレット式AppleScriptランチャー。知る人ぞ知る超便利機能!!!

■FastScript 3(Red Sweater Software)


Script Menuの強化版。よく使うScriptを上に表示するとか、キーボードショートカットを設定して呼び出せる。キーワード検索によりScriptの絞り込みがインクリメンタルに行えるなど、大量のScriptを効率的に呼び出すための機能を備える。

AS実行環境としてはまだこなれていないService Station

Program Name Name of runtime Support AppleScript document format AS Format NSAlert dialog is displayed in frontmost Can use GUI Scripting functions Can call AppleScript Libraries? Can call AS Library including Frameworks? Can call Generic Cocoa Functions? Can use Cocoa system notification functions?
Service Station osascript Script/Text Script/Applet Script/Text Script No Yes Yes No Yes No
Script Menu osascript Script/Scriptd/Applet Script/Scriptd Yes Yes Yes No Yes Yes
Shortcuts Events MacHelper File Embedded Script Text Yes Yes Yes No Yes No
Switch Control Assistive Control File Embedded Script Script (archived) Yes Yes No No Yes No
FastScript 3 FastScripts Script Runner Script/Scriptd/Applet Script/Scriptd Yes Yes Yes No Yes

GUIなしプロセス中でosascript経由でAppleScriptの実行を行なっているので、Service StationだけNSAlertによるダイアログ表示が最前面に出てこないという問題があります。

ホームディレクトリ下のAppleScriptライブラリは呼べるし、Cocoaの機能やGUI Scriptingの機能も呼べる、標準的な環境に近いレベルにはあると思われます。

実行Scriptにバンドル形式のAppleScript(.scptd)を指定できないというのは、激弱としかいいようがありませんが、作者がバンドル形式のAppleScriptのことを知らないことが原因のように見えます。

コンテクストメニューからAppleScriptを呼び出せるようにする、というのは他のツールに見られない特徴といえますが、いまScript Menuに入れている膨大なファイル処理系のScriptを、そのままコンテクストメニューから呼べるようにすると、邪魔すぎます(膨大なScriptが表示されて上下にスクロールしまくり)。

コンテクストメニューへの階層メニュー表示をサポートしないと、自分は他のツールで使っているScriptを移行させられません。ただ、そこまでメニューにAppleScriptをホスティングしているユーザーはそうそういなさそうなので、機能強化してもそれに追いついてこられるユーザーが少なさそう。

作業で使うAppleScriptを、その場でちょっとだけ呼び出すようにして運用しないと、大量のAppleScriptがあふれかえって運用そのものが行えません。使い所が難しいツールといえるのかも? 

自分が「欲しい」と思える機能は、結局自分で作るしかなさそうな気配がしています。


▲Script Menuから呼び出している、Finder上で選択中のファイルに対して実行するScript群。こんなのコンテクストメニューには表示し切れない

Posted in Review | Tagged 12.0savvy Service Station | Leave a comment

マウスの右クリックメニューをカスタマイズするService Station

Posted on 1月 26, 2022 by Takaaki Naganoya

マウスの右クリックで表示されるコンテクストメニューをカスタマイズするツール、「Service Station」(Version 2020.9 (23))を購入して、macOS 12.2+M1 Mac miniの環境で試してみました。

同ツールはMac App Storeで販売されており、フリーでダウンロードしたのち、App内購入(1,840円)で購入できます。

OS標準のScript MenuにFinder上で選択中のファイルを対象としたAppleScriptを大量に仕込んで使っていますが、やはりコンテクストメニューから呼び出せたほうが便利なので、試してみることにしました。

Mac App Storeからダウンロードした状態(フリーで使えるお試し版)ではAppleScriptの起動はできないため、App内購入が必要です。

サポートページ(Github)、Twitterアカウントなどが公開されています。

この手のツールは、AppleScriptに対する知識が少ないデベロッパーが開発したものが多く、実際に使ってみると不満を覚える確率がきわめて高いジャンルです。これまでにも、実際に試すと「使い物にならない」と判断したツールがほとんどであるため、十分な検証が必要でしょう。

# デフォルト状態でAppleScriptの実行を指定できないのは、試用版の制限なのでご注意ください。

Service StationでAppleScript書類をコンテクストメニューから処理してみよう

本ツールのUser Interfaceはきわめてシンプルです。「Rules」でどのようにファイルを特定し、「Menu Items」でオープンするアプリケーションやAppleScriptを指定します。

初期状態では、「Folders」「Images」「Text」の3つのRulesが設定されています。ここに、AppleScript書類を処理するRulesを追加してみましょう。

どのような方法でAppleScript書類を特定できるのか、Rulesのポップアップを調べてみると……

「Kind -> Script」で「AppleScript」を指定するとよさそうです。Rulesを指定したあとで、「Menu Items」にサンプルScriptを指定してみましょう。特定のハンドラ宣言を記述したAppleScriptをここに指定できるようになっています(フラットなScriptだけであってバンドル形式は予想どおり実行できません)。


▲実行Scriptとしてバンドル形式のAppleScript書類(.scptd)を認識しないので、バンドル内に実行バイナリやAppleScriptライブラリを突っ込むのは無理

Service Stationで実行指定するAppleScriptは、~/Library/Application Scripts/com.knurling.ServiceStation.Attendant フォルダに入れる必要があります。

Finder上で試してみると…..AppleScript書類(.scpt、.scptd)を認識しません(ーー;;;


▲この設定にしたくなるが、この指定方法だとAppleScript書類(.scptおよび.scptd)を認識しない。罠なのでこの設定はしてはいけない。作者は.applescriptのテキスト形式のAppleScriptしか試していないもよう

この作者は「AppleScript書類」をテキスト形式(.applescript)でしかチェックしていないようです。なので、さきほどの「Rules」で提示された「Kind -> Script is AppleScript」は罠(Trap)なので選択してはいけません。

拡張子で指定しても応答しません(scpt、scptd)。RulesでUTIを指定できるようなので、

フラットなScript書類「com.apple.applescript.script」、バンドルScript書類「com.apple.applescript.script-bundle」、Script DebuggerのバンドルScript書類「com.latenightsw.osa.bundle」をAny条件で(どれかがヒットしたら該当したものとみなす)登録してみました。

はい。今度は問題なく、Finder上でAppleScript書類を選択した状態で表示させた(マウスの右クリック)コンテクストメニューでサンプルAppleScript(処理用のハンドラを記載しただけ)がメニューに表示され、添付のサンプルScript(「System Setup」タブの「Sample Scripts」ボタンを押すと表示される)を実行できました。

Service Station経由で実行したAppleScript(サンプルScript)は以下のような内容です。

AppleScript名:AppleScript.scpt
on serviceStationDidSelect(targetedURL, selectedItemURLs, menuKind)
  display dialog "targetedURL:
" & targetedURL & "

" & "selectedItemURLs:
"
& selectedItemURLs & "

" & "menuKind:
"
& menuKind
end serviceStationDidSelect

★Click Here to Open This Script 

ランタイム環境はosascript

お約束で、Service StationのAppleScriptランタイム環境名は知っておかなければいけないので、

AppleScript名:Runtime名を表示.scpt
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

on serviceStationDidSelect(targetedURL, selectedItemURLs, menuKind)
  set procInfo to current application’s NSProcessInfo’s processInfo()
  
set aName to procInfo’s processName() as string
  
display dialog aName
end serviceStationDidSelect

★Click Here to Open This Script 

こんなAppleScriptを書いて実験してみると….

のように、「osascript」を呼び出して実装していることがわかりました。

NSAlertダイアログを表示するも、最前面に表示されず

ランタイム環境が「osascript」だと、自前でアラートダイアログを表示させたときに「最前面に表示されない」といった問題が生じる可能性があります(osascriptでお手軽実装するの勘弁してほしい。Script側に苦労を押し付けないでほしい)。あとは、GUIなしプロセスで実行するようなので、GUI Scriptingの認証を通せるか不明です。

また、どこぞのネット上のサーバー上のREST APIにアクセスしようとしてもセキュリティ上の制約で「アクセスできない」可能性もあります(実行プロセス側でネット接続の許可をもらっていないと無理)。今後も検証が必要です。

現時点でみつけた「おや?」という仕様は、Rulesでルールを作った状態で複数のAppleScript書類を選択すると、コンテクストメニューにService Stationの項目が出てこないという点です。画像の複数選択には反応するようですが、自前で設定したRulesだと複数ファイル選択時に応答しません。

# これは、追試によって設定アプリケーションを終了させるまで設定が反映されない部分があることが判明

また、Service StationのRulesから呼び出すAppleScriptのハンドラで、受け渡されるのはHFS path stringであってURLではありませんし(fURLなのか?)、複数のURLを受信できるような表記がありながらも、本バージョンでは1つのURL(正確にはHFS path)しか受け渡されません。

# 追試の結果、複数のパス(fURL)が受け渡されていることが判明

本ツールの開発者はAppleScriptを日常的に書いていない気配が濃厚ですが、それでも修正されれば我慢できないというほどでもないでしょう。


▲予想どおり、NSAlertのダイアログを表示してもランタイムがosascriptなので最前面に表示されなかった(GUIなしの不可視プロセスからosascriptを実行した際の挙動)。CDN上のJavaScriptライブラリの呼び出しは行えた

パラメータの型チェックを試してみました。

AppleScript名:パラメータの型チェック.scpt
on serviceStationDidSelect(targetedURL, selectedItemURLs, menuKind)
  set c1 to (class of targetedURL) as string
  
set c2 to (class of selectedItemURLs) as string
  
set c3 to (class of menuKind) as string
  
  
display dialog "targetedURL:
" & c1 & "

" & "selectedItemURLs:
"
& c2 & "

" & "menuKind:
"
& c3
end serviceStationDidSelect

★Click Here to Open This Script 

サンプルScriptの記述がよくないせいで勘違いしてしまったようです。targetedURLはfURL、selectedItemURLsはList、menuKindはintegerで返ってきていました。

ホームディレクトリ内のAppleScriptライブラリを呼び出す

コンテクストメニューから呼び出すAppleScript内で、ホームディレクトリ以下のAppleScriptライブラリを呼び出すことができるか確認してみました。

結論:呼べる

AppleScript名:AppleScript v2_with_Library.scpt
use AppleScript version "2.4"
use scripting additions
use radioLib : script "displayTextView"

on serviceStationDidSelect(targetedURL as «class furl», selectedItemURLs as list, menuKind as integer)
  set aStr to do shell script "cal 2022"
  
display text view aStr main message "Main Message" sub message "Sub Message" with properties {font name:"Courier", size:13, width:600, height:300}
end serviceStationDidSelect

★Click Here to Open This Script 

Posted in news Review | Tagged 12.0savvy NSProcessInfo Service Station | Leave a comment

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • UI Browserがgithub上でソース公開され、オープンソースに
  • macOS 13 TTS Voice環境に変更
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • AS関連データの取り扱いを容易にする(はずの)privateDataTypeLib
  • macOS 13でNSNotFoundバグふたたび
  • macOS 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題
  • 新発売:iWork Scripting Book with AppleScript
  • ChatGPTでchatに対する応答文を取得
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (186) 13.0savvy (59) CotEditor (60) Finder (47) iTunes (19) Keynote (99) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (57) Pages (38) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC