MacOS - Osascript ile Dinamik Buton Oluşturmak


Dinamik buton kullanımını kısa anlatmak isterdim fakat, bu örnek güzel çalıştığı için bu örnek üzerinden gitmek istedim.

Aşağıdaki uzun ve karışık gibi görünen kod blogunda kullanıcının mac cihazındaki bazı uygulamaların yüklü olup olmadığını veya kart okuycunun takılı olma durumunu kontrol ediyorum. "checkAPP" metodunda bu işlemleri yapıyorum. (Kontrol yöntemim doğru değil herzaman net sonuç vermez biliyorum bir kaç sene önce böyle yazmıştım sonra üzerine yoğunlaşma fırsatım olmadı)    

 Ardından bu durumları bir değişkene atıyorum. Bu değişkenlerin durumuna göre ilgili uygulamayı yükleme isminde değişkenler oluşturuyorum.  "createbutton" fonksiyonunda bu işlemleri yapıyorum. bu metotun sonunda ise eğer iki uygulamada aynı anda yüklü ise sorun yok fakat bir tane buton boş kalacaksa o butona "yenile" stringini atıyorum. Eğer iki uygulamada yüklü ve iki butonda boş kalacaksa kontrol edip birine yine "yenile" ifadesini atıyorum.

E imza araçlarının kurulumunu içeren benim bu örneğim imzalama araçlarının her birinin bir metodu var.Bunlar ExampleAppYUKLE, ExampleSignServiceYUKLE. Hangisi yüklü değilse onu kurabilmek için metot metot ayırmak zorundaydım.

En eğlenceli yerine gelelim. eIMZA fonksiyonunda yüklü olup olmadığını kontrol ettiğim değişkenleri boş string olarak ayarlıyorum. ardından döngü içerisinde checkapp ile yüklü olma durumlarını kontrol ediyorum ve boş ayarlardığım değişkenlerin değerleri doluyor bunu osascript ile ekranda gösteriyorum, aynı scriptin sonlarında buttons bölümünde buton eklerken Yukarda gerek boş gerekse "yenile" olarak set edilen butonları veriyorum. Birde eğer imza kurulu ise test edebilmeleri için test butonu koydum.

Test ekranına gelindiğinde ise yine test ekranları ile alakalı butonlar oluşturuyorlar nerede test etmek istediğini yada işlem yapmadan çıkışı seçiyor. 

 Kısacası kurulu olmayan programı tespit edip, kur butonu oluşturuyor. eğer hepsi kurulu ise yenile ve test butonu gösteriliyor. 


-----------------------------------------------------------------------------------------------------------------------------------------

function checkAPP() {
    acs=$(system_profiler SPSmartCardsDataType |grep "ExampleCardReader")
    APP=$(ls /Applications |grep "ExampleApp")
    APPSIGN=$(ls /Applications |grep "ExampleSignService")

    if echo "$acs" | grep -q "(no card present)"; then
        USB="Kart_Okuma_Başarısız"
    elif echo "$acs" | grep -q "length"; then
        USB="Kart_Okuma_Başarılı"
    elif [ -z "$acs" ]; then
        USB="USB_Takılı_Değil"
    fi

    if echo "$APP" | grep -q "ExampleApp"; then
        AKIA="Yüklü"
    else
        AKIA="Yüklü_Değil"
    fi

    if echo "$APPSIGN" | grep -q "ExampleSignService.app"; then
        APPSIGN="Yüklü"
    else
        APPSIGN="Yüklü_Değil"
    fi
}

function createbutton(){
    if [[ $AKIA = "Yüklü_Değil" ]]; then
        Button1="ExampleApp_Yükle"
    else
        unset Button1
    fi

    if [[ $APPSIGN = "Yüklü_Değil" ]]; then
        Button2="ExampleSignService_Yükle"
    else
        unset Button2
    fi

    if [[ -z $Button1 ]]; then
        Button1="Yenile"
    elif [[ -z $Button2 && $Button1 = "ExampleApp_Yükle" ]]; then
        Button2="Yenile"
    fi
}

function ExampleAppYUKLE() {
    echo "ExampleApp Yükleme Komutu"
    password=$(osascript <<EOF
    set dialogTitle to "ExampleApp ve Java Kurulumu için Parola Girişi"
    set passwordDialog to display dialog "Yükleme İşlemi için cihazınızın parolasını girin:" default answer "" with hidden answer buttons {"İptal", "Tamam"} default button "Tamam" with title dialogTitle
    set buttonPressed to button returned of passwordDialog
    set passwordResult to text returned of passwordDialog
    if buttonPressed is "İptal" then
        error "Kullanıcı İptal etti."
    else
        passwordResult
    end if
EOF
)
    osascript -e 'display alert "ExampleApp ve Java Kurulum işlemi başladı."'
    download "https://example.com/exampleapp.zip" "$DownloadsDirectory/exampleapp.zip"
    osascript -e 'display alert "İndirme tamamlandı."'
    echo "$password" | sudo -S unzip -o $DownloadsDirectory/exampleapp.zip -d $DownloadsDirectory
    osascript -e 'display alert "Zip açıldı. Yükleme işlemi başlıyor."'
    echo "$password" | sudo -S installer -pkg "$DownloadsDirectory/exampleapp/ExampleJava.pkg" -target /
    echo "$password" | sudo -S cp -r $DownloadsDirectory/exampleapp/ExampleApp /Applications
}

function ExampleSignServiceYUKLE() {
    password=$(osascript <<EOF
    set dialogTitle to "ExampleSignService Kurulumu için Parola Girişi"
    set passwordDialog to display dialog "Yükleme İşlemi için cihazınızın parolasını girin:" default answer "" with hidden answer buttons {"İptal", "Tamam"} default button "Tamam" with title dialogTitle
    set buttonPressed to button returned of passwordDialog
    set passwordResult to text returned of passwordDialog
    if buttonPressed is "İptal" then
        error "Kullanıcı İptal etti."
    else
        passwordResult
    end if
EOF
)
    download "https://example.com/examplesignservice.pkg" "$DownloadsDirectory/examplesignservice.pkg"
    echo "$password" | sudo -S installer -allowUntrusted -pkg "$DownloadsDirectory/examplesignservice.pkg" -target /
    osascript -e 'display notification "ExampleSignService Yükleme Tamamlandı" with title "Example Kurulum" subtitle "Kurulum Tamamlandı"'
}

function eIMZA() {
    USB="";
    AKIA="";
    SIGNAPP="";
    while true; do
        checkAPP;
        createbutton;
        secim=$(osascript -e 'with timeout of 30 seconds' -e 'tell application "System Events" to button returned of (display alert "Example Kurulum" message "USB İmza: '$USB'\nExampleApp: '$AKIA'\nExampleSignService: '$APPSIGN'" without icon buttons {"'$Button2'","'$Button1'","İmza Test"})' -e 'end timeout')
        if [[ $secim = "ExampleApp_Yükle" && $Button1 != "Yenile" ]]; then
            ExampleAppYUKLE
        elif [[ $secim = "İmza Test" ]]; then
            # İmza Test alt menüsü
            sec=$(osascript -e '
            tell application "System Events"
                button returned of (display alert "İmza Test Seçenekleri" message "Çalıştığınız kurumun test sayfasını seçiniz." buttons {"Çık", "Examp", "Example"} default button "Examp")
            end tell')
            if [[ $sec = "Examp" ]]; then
                [[ $APPSIGN = "Yüklü" ]] && open /Applications/ExampleApp.app
                open https://example.com/Examp/testpage
            elif [[ $sec = "Example" ]]; then
                [[ $APPSIGN = "Yüklü" ]] && open /Applications/ExampleSignService.app
                open https://example.com/Example/testpage
            elif [[ $sec = "Çık" ]]; then
                break
            fi
        elif [[ $secim = "$Button2" && $Button2 != "Yenile" ]]; then
            SIGNARTYUKLE
        elif [[ $secim = "Yenile" ]]; then
            continue
        elif [[ $secim = "İptal" || -z $secim ]]; then
            break
        fi
    done
}

eIMZA

-----------------------------------------------------------------------------------------------------------------------------------------

Osascriptte en fazla 3 buton olabileceği konusu düşündürsede bu örnek benim senaryomda çok işime yaramıştı.

Umarım okuyana da faydası olur.

Scriptleri örnek amaçlı paylaşıyorum. Olası güvenlik açıkları veya kod hatalarından doğabilecek sorunların sorumluluğu tamamen kullanıcıya aittir.
Devamını Okumak İçin »