LoginSignup
0
0

More than 5 years have passed since last update.

NSPasteboardでsetStringした際に戻り値をtrueにできた話

Last updated at Posted at 2019-05-04

共有したいこと

インフラエンジニアである自分がコピペで楽したいのでmacOSアプリを自作したのですが、その際にクリップボードへ文字列を格納する際に解決した点を共有します。

具体的に

  1. macOSのクリップボードを操作するClassであるNSPasteboardにはfunc setStringがあることを確認。
  2. よーし、setStringでstringをsetしちゃうぞ、ということでNSPasteboard.general.setString(anyString, forType: NSPasteboard.PasteboardType.string)なイメージで実装。
  3. Boolを返すそうな、ということでprint(NSPasteboard.general.setString(anyString, forType: NSPasteboard.PasteboardType.string))なイメージで実装アップデート。ブレイクポイントも設定したけど。
  4. 実行!
  5. 結果falseが返る・・・。func setStringによるとotherwise false if ownership of the pasteboard has changed.とのこと。
  6. うん。分からん!(ダメ人間)
  7. クリップボードにsetできない理由って何だろう・・・妄想しつつNSPasteboardを眺めてみる。
  8. んー、func clearContents()ってある。中身をクリアしてからsetしたら動くかなという直感。
  9. NSPasteboard.general.clearContents()してからprint(NSPasteboard.general.setString(anyString, forType: NSPasteboard.PasteboardType.string))な感じで実装。
  10. 実行!
  11. 結果trueが返る!幸せ。
  12. 改めてfunc clearContents()を読んでみるとDiscussionに、Clears the existing contents of the pasteboard, preparing it for new contents. This is the first step in providing data on the pasteboard.って、ちゃんと書いてある。
  13. 結論、公式のドキュメントはよく読めと・・・。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0