macOSアプリでバンドルされているPDFを表示する

macOSアプリでバンドルされているPDFを設定されているアプリケーションで開く方法についてまとめました。

環境

バンドルされているPDFを表示する

//バンドルされているPDFを取得する
if let filePath = Bundle.main.path( forResource: "Manual", ofType: "pdf" ) {

    //設定されているアプリケーションで開く
    if NSWorkspace.shared.openFile( filePath ) {
            
        print("OK")
            
    } else {
            
        print("Error")
            
    }
        
}

macOSアプリでメニューを選択可能または、選択不可能にする

macOSアプリのメニューをグレーアウトして選択をできないようにする方法についてまとめた。

メニューの選択可能・選択不可能

class AppDelegate: NSObject, NSApplicationDelegate {

    
    //メニューの選択可能・選択不可能
    @objc func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
        
        if item.action == #selector(AppDelegate.MenuTest(_:)) {
            //選択不可能(グレーアウト、選択をできないようにする)
            return false
        }

        return true

    }

    //メニューの処理
    @IBAction func MenuTest(_ sender: Any) {
            //処理
    }

}

参考

https://stackoverflow.com/questions/33594837/menubar-with-storyboard-validatemenuitem-not-get-called

C#で、Quoted-Printable(メールの件名(Subject))のデコード

メールの件名(Subject)が、Quoted-Printableになっている場合、「=XX=XX=XX」のように「=」と2桁の16進数文字コードの組み合わせになっています。 「=」の後の2桁の16進数文字コードをbyteに変換後、System.Text.Encodingを使って日本語に変換できます。

using System;
using System.Text;

private static string DecodeQuotedPrintable( string input, string charset )
{
    List<byte> data = new List<byte>();
    string code;
    int pos0 = 0;
    int pos1 = 0;

    while( pos0 < input.Length )
    {
        pos1 = input.IndexOf( "=", pos0 );

        if( pos1 < 0 || pos1 + 3 > input.Length )
        {
            data.AddRange( Encoding.ASCII.GetBytes( input.Substring( pos0 ) ) );
            break;
        }

        if( pos1 != pos0 )
        {
            data.AddRange( Encoding.ASCII.GetBytes( input.Substring( pos0, pos1 - pos0 ) ) );
        }

        code = input.Substring(pos1 + 1, 2);

        if( Uri.IsHexDigit( code[0] ) && Uri.IsHexDigit( code[1] ) )
        {
            data.Add( (byte)Convert.ToInt32( code, 16 ) );
            pos0 = pos1 + 3;
        }
        else
        {
            data.Add( (byte)input[pos1] );
            pos0 = pos1 + 1;
        }
    }

    return Encoding.GetEncoding( charset ).GetString( data.ToArray() );
}

string utf8 = DecodeQuotedPrintable( "=E6=9D=B1=E4=BA=AC", "utf-8" );
Console.WriteLine( "utf-8: {0}", utf8 );

string shiftjis = DecodeQuotedPrintable( "=93=8C=8B=9E", "shift-jis" );
Console.WriteLine( "shift-jis: {0}", shiftjis );

string eucjp = DecodeQuotedPrintable( "=C5=EC=B5=FE", "euc-jp" );
Console.WriteLine( "euc-jp: {0}", eucjp );

結果

utf-8: 東京
shift-jis: 東京
euc-jp: 東京

CensOS7で、monitによるプロセス監視

CensOS7で、monitによるプロセス監視(プロセスが止まったら再起動する)について、まとめた。

インストール方法

rpm -Uvh http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
yum -y install monic

自動起動の設定

systemctl enable monic

プロセス監視の設定

設定を/etc/monit.dディレクトリに作成する
例:vi /etc/monit.d/program1

check process diced matching “program1”
  start program = "/usr/local/bin/test/program1"
  stop  program = "/usr/bin/pkill program1"
  if 5 restarts within 5 cycles then unmonitored

CentOS7のMongoDBをC++から利用できるようにする

パッケージのインストール

yum -y install boost boost-devel git-core gcc-c++ glibc-devel


sconsのインストール

sconsをソースコードからインストールする

wget http://prdownloads.sourceforge.net/scons/scons-2.3.5.tar.gz
tar zxf scons-2.3.5.tar.gz
cd scons-2.3.5
python setup.py install


MongoDB C++ドライバーのインストール

MongoDB C++ドライバーのダウンロードと解凍

wget https://github.com/mongodb/mongo-cxx-driver/archive/legacy.zip
unzip legacy.zip
cd mongo-cxx-driver-legacy

ビルド

sudo scons

インストール

scons --prefix=/usr/local install


確認

tutorial.cpp を下記の内容で作成する

#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h" // for the driver

void run() {
  mongo::DBClientConnection c;
  c.connect("localhost");
}

int main() {
    mongo::client::initialize();
    try {
        run();
        std::cout << "connected ok" << std::endl;
    } catch( const mongo::DBException &e ) {
        std::cout << "caught " << e.what() << std::endl;
    }
    return EXIT_SUCCESS;
}

ビルド

g++ tutorial.cpp -pthread -lmongoclient -lboost_thread -lboost_filesystem -lboost_regex -lboost_program_options -o tutorial

実行

./tutorial

結果

connected ok


参考

下記のWebページを参考にしました。

Tutorial · mongodb/mongo-cxx-driver Wiki · GitHub

CentOS7のMongoDBをPHPから利用できるようにする

パッケージのインストール

yum -y install php php-devel php-mbstring php-pear gcc gcc-c++ openssl-devel


peclでモジュールのインストール

pecl install mongo


PHPの設定

/etc/php.d/mongo.ini を下記の内容で作成する

; Enable mongo extension module
extension=mongo.so

CentOS7にMongoDBをインストールする

リポジトリの追加

/etc/yum.repos.d/mongodb-org-3.0.repo を下記の内容で作成する

[mongodb-org-3.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
gpgcheck=0
enabled=1


インストール

yum install mongodb-org


起動

systemctl start mongod


起動時に自動起動するように設定

mongodは、systemctl で自動起動の設定ができないので、chkconfigを使用する

chkconfig mongod on


ファイアウォールの設定

27017ポートを開放する

firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload