Applescript file I/O Issue

Discussion in 'Mac Programming' started by LtRammstein, Dec 29, 2008.

  1. LtRammstein macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Location:
    Denver, CO
    #1
    Hey all!

    I have an issue with reading a text file in Applescript. This is for an Adium script, and the file is in the same folder as the script itself.

    Here's a snippet of what I'm trying to do:
    Code:
    on ReadFile()
    	log "Reading file..."
    	tell application "Finder"
    		try
    			set fileName to "macgames.txt" as Unicode text
    			log "Filename: " & fileName
    			set fileID to open for access fileName
    			log "File ID: " & fileID
    			set dataFromFile to read fileID
    			log "(Try Block)Data: " & dataFromFile
    		on error
    			log "(On Error Block)Data: " & dataFromFile
    			--close access fileID
    		end try
    		close access fileID
    	end tell
    end ReadFile
    
    The log is just to allow me to see what variable has what, and to tell me what part of the code I am in. It seems that the script knows the file is there, but when running I get this:
    Code:
    open for access "macgames.txt"
    		2816
    	(*File ID: 2816*)
    	read 2816
    	(*(On Error Block)Data: *)
    	close access 2816
    	(*Start compare loop...*)
    	(*End of compare loop...*)
    	(*Return application name: Script Editor*)
    
    So the log "(On Error Block)Data:" means no data is being presented! I want it to come out as a list, but I just want data to show up first. Any suggestions on fixing this issue?

    Thanks for any help!

    PS: I created the text file in TextEdit and did modifications using VIM.
     
  2. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #2
    You need to get the path to the app/script first and append the file name to that. Adium might have a way to get the scripts folder, you can check its dictionary.

    Otherwise here is how to get the path to the app:

    Code:
    set myPath to (path to me)
     

Share This Page