マウスカーソルの移動について

環境:macOS High Sierra 10.13.1, Xcode 9.1, Swift 4

プログラムによってマウスカーソルを自動で移動させたい時、次のようなコードで実行していました。

let screenPoint = CGPoint (移動させるマウスカーソルのスクリーン座標)

if let mouseMoveEvent = CGEvent (mouseEventSource: nil, mouseType: .mouseMoved,

mouseCursorPosition: screenPoint, mouseButton: .left) {

mouseMoveEvent.post (tap: cghidEventTap)

}

確かではありませんが、Xcode 8.0 くらいまでは動作していたと思いますが、現バージョンにおいて

動作していない事に気が付きました。

Deprecated の関数等も使用していません。

対策の為の良い知恵をいただければ幸いです。

そもそもコード自体に問題が有るのか、代替案が有るのかも含めてご回答をお願いいたします。

iMac (27-inch, Late 2012), macOS High Sierra (10.13.1)

投稿日 2017/11/04 21:18

返信
スレッドに付いたマーク ランキングトップの返信

投稿日 2017/11/05 03:20

Window系のエラーはあまり関係ないですね。ニューアプリを作れば5分で確認できると思います。

ユーザがアップロードしたファイル

あっ、因みに、Sandbox化されたアプリでは動かないです。これ系のexceptionキーはないようなので、Sandboxはオフにする必要があります。(Xcode9では、デフォルトでSandboxがOnになってます。)


·Posting keyboard or mouse events to another app


You cannot sandbox an app that controls another app. Posting keyboard or mouse events using functions like CGEventPost offers a way to circumvent this restriction, and is therefore not allowed from a sandboxed app.


https://developer.apple.com/library/content/documentation/Security/Conceptual/AppSandboxDesignGuide/DesigningYourSandbox/DesigningYourSandbox.html

返信: 4
スレッドに付いたマーク ランキングトップの返信

2017/11/05 03:20 MC-8 への返信

Window系のエラーはあまり関係ないですね。ニューアプリを作れば5分で確認できると思います。

ユーザがアップロードしたファイル

あっ、因みに、Sandbox化されたアプリでは動かないです。これ系のexceptionキーはないようなので、Sandboxはオフにする必要があります。(Xcode9では、デフォルトでSandboxがOnになってます。)


·Posting keyboard or mouse events to another app


You cannot sandbox an app that controls another app. Posting keyboard or mouse events using functions like CGEventPost offers a way to circumvent this restriction, and is therefore not allowed from a sandboxed app.


https://developer.apple.com/library/content/documentation/Security/Conceptual/AppSandboxDesignGuide/DesigningYourSandbox/DesigningYourSandbox.html

2017/11/05 06:59 hohokihai への返信

hohokihai さん、返信ありがとうございました。


Sandbox を Off に設定したところ動作しました。

macOS Deployment Target は変更していません。


ご教授いただいた事で期待する結果が得られました。大変感謝いたします。

しかし、Sandbox に関しての課題が出てきました。勉強します。

2017/11/05 02:32 hohokihai への返信

hohokihai さん返信ありがとうございます。


まずは訂正をさせていただきます。

ご指摘されました"cghidEventTap"は".cghidEventTap"でした。タイプミスです。

また、macOS Deployment Target を 10.12 にしたところ、ビルド後に

Unknown Window class (null) in Interface Builder file, creating generic Window instead

が出力されました。

実行結果に変化はありませんでした。

しばらくは試行錯誤してみます。

Music Maker[1049:26162] Unknown Window class (null) in Interface Builder file,

creating generic Window instead

このスレッドはシステム、またはAppleコミュニティチームによってロックされました。 問題解決の参考になる情報であれば、どの投稿にでも投票いただけます。またコミュニティで他の回答を検索することもできます。

マウスカーソルの移動について

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple Account を使ってご参加ください。