0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

macOSのプリンタをターミナルから制御する

Posted at

プリンタ設定

macOSでWindowsのADユーザでログオン時にデフォルトプリンタを自動で設定する必要が出てきたのでメモ。

環境

  • MacBook Air ( macOS Big Sur )

ログオンスクリプトの実装

LoginHook使って実装する。なお___LoginHookを使うことは2014年に非推奨となっています。___
LoginHookは実行時に引数としてログオンしたユーザ名を渡すみたい。

root.sh
# !/bin/bash
usrName=$1
sudo -u $usrName /Library/Hook/user.sh $1

defaultsでスクリプトをLoginHookに登録する。

sudo defaults write com.apple.loginwindow /Library/Hook/root.sh

デフォルトプリンタを指定する

lpstat -sでプリンタ名を調べる。

lpoptions -d "Printer_Name" で指定。

と思ったら…

指定されてない。検証してたら、「デフォルトのプリンタ」が「最後に使用したプリンタ」に設定されている場合はlpoptionsで変更が適用されないことがわかった。
スクリーンショット 2021-03-30 11.29.10.png

CUPSの設定

印刷システムのCUPSのdefaults設定を見てみると、どうもここに最後に使用したプリンタの設定が書き込まれているらしいことがわかった。

{
    LastUsedPrinters =     (
                {
            Network = "192.168.1.254";
            PrinterID = "Default_Printer_ID";
        }
    );
    UseLastPrinter = 1;
}

このUseLastPrinterの値をfalseに書き換えてやると、lpoptionsが適用されるようになる。

最終的なスクリプト

user.sh
# !/bin/bash
defaults write org.cups.PrintingPrefs UseLastPrinter -bool False
lpoptions -d "Printer_Name"
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?