7 Replies Latest reply: Jun 5, 2012 11:05 AM by BDAqua
ndubau Level 1 Level 1 (0 points)

Hello

I'm a french user, very novice in scripting. In an application I created, a special font is required. So I wrote an applescript to install it in the FontBook. This is my code :

 

-- Chemin d'accès du présent script

set theScriptPath to path to me

 

-- chemin du dossier actuel

tell application "Finder"

    set my_folder_path to container of (path to me) as text

   

    -- Chemin d'accès à la police

    set theFontPath to my_folder_path & "IDAutomationHC39M.ttf"

end tell

 

set theFont to theFontPath

 

-- installation police

tell application "Finder"

    open theFontPath

end tell

 

tell application "Font Book"

   

    if installation target is not font domain "User" then

        set installation target to font domain "User"

    end if

   

    activate

    set theFontWindow to the first window

end tell

 

 

tell application "System Events"

    tell process "Font Book"

        tell window 1

            tell group 1

                click button "Installer la police"

            end tell

        end tell

        tell application "Font Book" to quit

    end tell

end tell

It's certainly not very pure, but it runs ! The only problem is when the font already exists for user or system, an alert appears to warn of the existence of a doubloon. How can I include a test : if the font exists do nothing else install the font ?

Thanks for your help

Noël


G4 1,25, Mac OS X (10.4.3)
  • 1. Re: Applescript : install a font
    BDAqua Level 10 Level 10 (116,475 points)

    Hello, here's some ways to tell if a file exists...

     

    http://macscripter.net/viewtopic.php?id=18736

     

    set msg to "no"

    tell application "Finder"

       if exists POSIX file "/usr/bin/gzip" then

           set msg to "yes"

       end if

    end tell

  • 2. Re: Applescript : install a font
    ndubau Level 1 Level 1 (0 points)

    Thanks for that answer.... But how can I define the path to the font folder ?

    Thankes

    Noël

  • 3. Re: Applescript : install a font
    BDAqua Level 10 Level 10 (116,475 points)

    Here's the locations of the Fonts, but you'd want to put yours in...

     

    ~/Library/Fonts/

     

    http://support.apple.com/kb/HT2435

  • 4. Re: Applescript : install a font
    ndubau Level 1 Level 1 (0 points)

    I had seen that page. I know the path to my user account /volumes/data/.....

    But when I distribue an app I can't know every final user path ! Is there a generic way to locate it ? or calculate it ?

    Thanks one more

    Noël

  • 5. Re: Applescript : install a font
    BDAqua Level 10 Level 10 (116,475 points)

    The ~ is the default path to the Home foler anywhere.

     

    ~/Library/Fonts/

     

    Should be the same on ny Mac regardless.

  • 6. Re: Applescript : install a font
    ndubau Level 1 Level 1 (0 points)

    Ok ! I understand... I tried the following adaptation of the script above

     

     

    set msg to "no"

     

    tell application "Finder"

       

        if exists POSIX file "~/Library/Fonts/IDAutomationHC39M.ttf" then

           

            set msg to "yes"

           

        end if

       

    end tel

    Font present or not I get the following answer and nothing in the result

     

    tell application "Finder"

        exists file ":~:Library:Fonts:IDAutomationHC39M.ttf"

            --> false

    end tell

    I told in the first message I was novice++++ Don't be angry !

    Noël

  • 7. Re: Applescript : install a font
    BDAqua Level 10 Level 10 (116,475 points)

    No expert here either, looks like Apple Script doesn't like the home folder shortcut of "`" as it cahanges it to...

     

              exists file ":~:Library:Fonts:IDAutomationHC39M.ttf"

     

    And no idea if that is the same, doesn't appear to be.

     

    Only thing I've worked out so  far is how to really get the Home folder...

     

    set myPath to (path to home folder) as string

    display dialog "myPath is " & myPath