LoginSignup
7
5

More than 3 years have passed since last update.

macOSでのスクリーンセーバの作り方

Last updated at Posted at 2020-08-02

macOSでのスクリーンセーバの作り方を調べた。以下を参考に作ってみた。
https://tolar.town/posts/2020/04/13/screensavers/

GitHub

まず最初に、GitHub。こちらを使うと、そのままスクリーンセーバが実行できるはず。
https://github.com/eto/DropRain

環境は、以下の通り。
- Xcode 11.6
- Swift 5.2.4

Xcodeを使っての作り方

  • Xcodeを起動→Create a new Xcode Project→macOS→Other→Screen Saverを選択→Next→Product Name:「DropRain」→Next→フォルダを指定する→Create
  • Cmd-1→「DropRain/DropRain」以下の→「DropRain.h」「DropRain.m」の2つを右クリック→Delete→Move to Trash(ここ重要。消さないと、こちらが使われてしまう。
  • Cmd-1→「DropRain/DropRain」を選択→Cmd-N→Swift File→Next→Save As:「DropRainView.swift」→Create→Don't Create
DropRainView.swift
import Foundation
import AppKit
import ScreenSaver
class DropRainScreenSaverView: ScreenSaverView {
    let view = DropRainView()
    override init?(frame: CGRect, isPreview: Bool) {
        super.init(frame: frame, isPreview: isPreview)
        self.animationTimeInterval = 1 / 30.0
        addSubview(view)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func startAnimation() {
        super.startAnimation()
    }
    override func stopAnimation() {
        super.stopAnimation()
    }
    override func draw(_ rect: NSRect) {
        super.draw(rect)
        var squareFrame = NSRect.zero
        squareFrame.size = NSSize(width: 150, height: 150)
        squareFrame.origin.x = (rect.width - squareFrame.width) / 2.0
        squareFrame.origin.y = (rect.height - squareFrame.height) / 2.0
        view.frame = squareFrame
    }
    override func animateOneFrame() {
        view.rotate(byDegrees: 1)
    }
}
class DropRainView: NSView {
    init() {
        super.init(frame: .zero)
        wantsLayer = true
        layer?.backgroundColor = CGColor.init(red:0.9, green:0.9, blue:0.9, alpha: 1.0)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

ビルド

  • Cmd-Bでビルド。Cmd-1→「DropRain/Products/DropRain.saver」を右クリック→Show in Finder
  • 「DropRain.saver」をダブルクリック→「このユーザ専用にインストールする」のまま「インストール」→「DropRain」を選択。ここで実行できているはず。

再インストール

  • 修正して再インストールする際には、通常は、Cmd-B→「DropRain.saver」を右クリック→Show in Finder「Day8.saver」をダブルクリック→「置き換え」とする。プレビュー画面内で修正後のプログラムが動いているはず。
  • が、キャッシュが効いてしまい、うまく更新されないことが多々あった。その場合は、以下の手順をとる。
  • 「システム環境設定」を終了。コマンドラインから以下を実行。(既存のスクリーンセーバも全部消してしまうので、注意。) rm -rf ~/Library/Developer/Xcode/DerivedData ~/Library/Screen\ Savers/*
  • Xcodeに戻り、通常操作を行なう。そうすると、キャッシュが消えて動くはず。

他のブログ

日本語で検索すると、以下のエントリーが出てくる。
- 2015.7.20 http://snippets.feb19.jp/?p=1549
- 2015年12月02日 https://qiita.com/kaneshin/items/cab5132517f6902824e3
- 2017/10/17 https://marunouchi-tech.i-studio.co.jp/4669/
が、この通りにやっても動かなかった。おそらく以前のversionだったため。上記の英語のエントリーは、現状で動くため、こちらを推奨する。

トラブル

  • 何度やってもうまく動かないケースがあった。「DropRain.h」「DropRain.m」の消し忘れで、こちらが効いてしまうという理由があった。
  • プログラムを改良する場合には、上記ブログにあるように、App形式にして、うまく動いてからスクリーンセーバにすると良い。

結論

もうスクリーンセーバは作らなくていいのではないか。

7
5
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
7
5