LoginSignup
0
1

More than 1 year has passed since last update.

macOS12.3でスクリプトが動かなくなったので修正した

Last updated at Posted at 2022-06-14

 先日、macOS12.3へのアップデートを実施したところ、これまで動いていたいくつかのスクリプトが動かなくなりました。調べてみたところどうやらPython2.x系のランタイム環境が削除された影響だったようです。今後のために対処した方法を置いておきます。

VivliostyleをローカルPC内サーバーで展開するコードの修正

 以前、こちらに書いたものを社内でもずっと使っているのですが、こちらでローカルWebサーバ起動処理にPython2系を使っていたために動かなくなりました。これは単純にPython3を使ってサーバを起動すればよいので、
python -m SimpleHTTPServer 8000

python3 -m http.server 8000

に書き換えて終了。以下に一応全文を置いておきます。

######vivliostyleフォルダ、表示するepubファイルの順でパスを指定するとepubファイルをVivliostyleで起動する。Mac専用。######
use utf8;
#Encodeモジュールをインポート
use Encode qw/encode decode/;

######Vivliostyleで表示する際のCSS追加指定######
my $addVivliostyleCss = "&renderAllPages=true&userStyle=data:,/*%3Cviewer%3E*/%0Aimg,%20svg%20%7B%20max-inline-size:%20100%25%20!important;%20max-block-size:%20100vb%20!important;%20object-fit:%20contain%20!important;%20%7D%0A/*%3C/viewer%3E*/%0A\@page%20:left%20%7B%0Amargin-right:%2010mm;%0A\@bottom-left%20%7B%0Acontent:%20counter(page);%0Amargin-left:%205pt;%0Amargin-bottom:%206mm;%0Awriting-mode:%20horizontal-tb;%0Afont-weight:%20normal;%0Afont-family:%20serif-ja,%20serif;%0A%7D%0A%7D%0A\@page%20:right%20%7B%0Amargin-left:%2010mm;%0A\@bottom-right%20%7B%0Acontent:%20counter(page);%0Amargin-right:%205pt;%0Amargin-bottom:%206mm;%0Awriting-mode:%20horizontal-tb;%0Afont-weight:%20normal;%0Afont-family:%20serif-ja,%20serif;%0A%7D%0A%7D%0A\@page%20:first%20%7B%0A\@bottom-left%20%7B%0Acontent:%20%27%27;%0A%7D%0A\@top-left%20%7B%0Acontent:%20%27%27;%0A%7D%0A%7D&spread=true"; #見開き表示、画像表示最適化、ノンブル表示の指定を追加
#############################################

#vivliostyleパッケージのパスを取得
my $vivliostyleFolderPath = $ARGV[0];
$vivliostyleFolderPath = decode('UTF-8', $vivliostyleFolderPath);
#パッケージの親ディレクトリのパスを取得(そこを起点にローカルWebサーバを起動する)
my $currentFolderPath = $vivliostyleFolderPath;
$currentFolderPath =~ s@^(.+?)/[^/]+$@$1@;
#vivliostyleフォルダ名を取得
my $vivliostyleFolderName = $vivliostyleFolderPath;
$vivliostyleFolderName =~ s@^.+?/([^/]+)$@$1@;

#表示するepubファイルのパスを取得
my $epubFilePath = $ARGV[1];
$epubFilePath = decode('UTF-8', $epubFilePath);

#乱数代わりに日時の数字を取得してepub解凍フォルダ名決定
my $getDateTimeNowCommand = 'date "+%Y%m%d%H%M%S"';
chomp(my $datetimenow = `$getDateTimeNowCommand`);
my $epubUnzipFoldername = "vivliostyleepubtmp_" . $datetimenow;
#テンポラリフォルダ無ければ作る
unless (-d $currentFolderPath . "/tmp"){mkdir $currentFolderPath . "/tmp"};
#テンポラリフォルダにEPUBファイルを解凍する
my $epubUnzipCommand = "unzip " . $epubFilePath . " -d " . $currentFolderPath . "/tmp/" . $epubUnzipFoldername;
system $epubUnzipCommand;

#ローカルWebサーバ起動処理
my $serverStartCommand = 'osascript -e \'tell application "Terminal" to do script "cd ' . $currentFolderPath . ';python3 -m http.server 8000"\'';
system $serverStartCommand;

#ディレイ処理
my $delayCommand = "osascript -e 'delay 2'";
system $delayCommand;

#Vivliostyle Viewer起動
my $openVivliostyleCmd = 'osascript -e \'tell application "Google Chrome" to open location "http://localhost:8000/' . $vivliostyleFolderName . '/viewer/index.html#b=http://localhost:8000/tmp/' . $epubUnzipFoldername . $addVivliostyleCss . "\"'";
system $openVivliostyleCmd;

PDFのページ数の取得

 PDFのページ数を取得して全ページPhotoshopで画像変換する処理をAppleScriptで自動化していましたが、ページ数取得にPython2系を使っていたため動かなくなりました。

 従来のコードはこちらの記事を参照にしたものです。これはPyObjCというものを通じて処理をしているようなので単純なPython3への置き換えではうまくいきそうにありません(無理でした)。ただ、この場合PDFのページ数が取れればよいので手段は何もPythonに限らずともよいはずです。ちょっと調べてみたところシェルでPDFのページ数を取得するためのかなりの手段が見つかりました
 このうち、とりあえず追加インストールが必要なさそうな「mdls」を使用する形で書き換えました(市川せうぞー師匠アドバイスありがとうございます)。Spotlight検索用のメタデータを使用するため信頼性は低いとのことなのですが、ユースケース的には支給されたPDFを画像変換し、変換後の画像は目視で確認しますのでまあうちの場合はこれで大丈夫でしょう。コード的には以下のような形です。

on run {pdfFilePathValue}
	set pagenum to my pdfPageCount(pdfFilePathValue)
end run

--- 与えられたPDFのページ数を返す
on pdfPageCount(aPDF)
	return (do shell script ("mdls -name kMDItemNumberOfPages -raw " & aPDF)) as integer
end pdfPageCount

 何か問題が出るようであればexiftoolあたりを使う形に置き換えてもよいかなと思っています。

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