Cannot find file in applescript

Discussion in 'Mac Programming' started by MyShout, Sep 19, 2014.

  1. MyShout, Sep 19, 2014
    Last edited by a moderator: Sep 19, 2014

    MyShout macrumors newbie

    Joined:
    Sep 19, 2014
    #1
    Hi,
    I have a small script that I want to use to copy a png file into the clipboard.
    Which I want to go like this.

    Code:
    set pngData to read file "copytoclipboard.png" as «class PNGf»
    set the clipboard to pngData
    
    In this case I want to put the compiled script in the same directory where the png file is, hoping that it would be able to find the file. Unfortunately that is not the case.

    If I give the thing a hard name like in the next example it will work.
    Code:
    set pngData to read file "Users:myshout:Library:myapplication:copytoclipboard.png" as «class PNGf»
    set the clipboard to pngData
    
    But this is not what I want as I want to be able to distribute the script in my application, so putting the compiled script next to the png file is the most secure way to distribute.

    Anyone knows how the apple script can figure out the file name from the directory from which it is executed ??

    Thank in advance,

    @+Maarten,
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Perhaps the path to section in the Commands Reference here might be of assistance.
     
  3. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
  4. MyShout thread starter macrumors newbie

    Joined:
    Sep 19, 2014
    #4
    This did it

    The following worked:

    Code:
    set aHome to ":Users"
    set aUser to system attribute "USER"
    set aFileName to "Library:StakePoint:stakepointclipboard.png"
    set totalName to aHome & ":" & aUser & ":" & aFileName
    set pngData to read file totalName as «class PNGf»
    set the clipboard to pngData
    Not sure if there are many macs around who rename there Users directory name, but asking for the system attribute "HOME" gives "/Users/" which gives me the problem to replace the "/" by ":"

    Thank you for the help.

    Regards

    @+Maarten
     

Share This Page