Missing fields in my custom .plist file

Hi everybody, I’ve started a couple of days ago to make my application.

This app moves mp3 files of a defined size to a choosen folder, and sorts them by month/year of download.
Its a simple tool for people who download lots of mp3 files and wants to keep the download folder clean as well.

What I need to do is to let the user choose an “Automatic mode” , that consist in:

  1. Reading the parameters used in my app and save them to a plist file (download folder path, destination folder, sort criteria, etc);

  2. Create a folder script that reads the plist file, and assign the script to user’s download folder.

Basically, there are 2 operating modes:

  • manual = the user execute it when he wants to;
  • automatic = the user sets everything once with the app, chooses automatic mode, and then every time a mp3 file is added to the download folder, it will be processed by my script.

---------------------- HERE IS THE PROBLEM ---------------------------

In my generated .plist file, i can see only my paths variables, but two variables are missing! They are values coming from 2 textboxes.

here is the code from the call, to plist generation.

   
 -- Exit button
on buttonExitClicked_(sender)
     set maxSizeValue to maxSize's stringValue()
     set minSizeValue to minSize's stringValue()
       makenewPlist()
       quit
end buttonExitClicked_
    
on makenewPlist()
     
    tell application "System Events"
        --init
          set the parent_dictionary to make new property list item with properties {kind:record}
          set the plistfile_path to "~/Desktop/example.plist"
          set this_plistfile to ¬
          make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
        
        --save variables to plist

        --working
          make new property list item at end of property list items of contents of this_plistfile ¬
          with properties {kind:string, name:"downloadsFolderPOSIX", value:downloadsFolderPOSIX}
          make new property list item at end of property list items of contents of this_plistfile ¬
          with properties {kind:string, name:"destinationFolderPOSIX", value:destinationFolderPOSIX}
        
        --not working (no results in plist)
          make new property list item at end of property list items of contents of this_plistfile ¬
          with properties {kind:string, name:"minSize", value:minSizeValue}
          make new property list item at end of property list items of contents of this_plistfile ¬
          with properties {kind:string, name:"maxSize", value:maxSizeValue}

    end tell

end makenewPlist_

This is the output:

<?xml version="1.0" encoding="UTF-8"?> destinationFolderPOSIX /Users/Funder/Desktop/ downloadsFolderPOSIX /Users/Funder/Downloads/
       [b] ??? WTF, MINSIZE & MAXSIZE ???[/b]

CURIOUS FACT: the same variables are working with no problems in another part of my script, so must be something related to plist generation(hehe i’m smart!) …

        do shell script "find " & downloadsFolderPOSIX & " -iname \"*.mp3\" -size +"&(minSize's stringValue())&"M -size -"&(maxSize's stringValue())&"M -execdir mv {} " & destinationFolderPOSIX & mp3craneGlobalFolder & monthYearFolder & "/ \\;"

This is the script that will be also in the “folder action script” … that’s why i need to recall the params from an external plist file!

My english is not perfect, but i hope that everything’s clear :slight_smile: sorry for this long post, and thanks for you help!!

Model: Macbook Pro
AppleScript: AppleScriptObjC & Xcode
Browser: Safari 537.36
Operating System: Mac OS X (10.8)

Hi,

try the Cocoa equivalent to create a property list file


       set plistDictionary to {minSizeValue:minSize, maxSizeValue:maxSize, destinationFolderPOSIX:destinationFolderPOSIX, downloadsFolderPOSIX:downloadsFolderPOSIX}
        set the plistfile_path to POSIX path of (path to desktop folder) & "example.plist"
        set plistData to current application's NSPropertyListSerialization's dataWithPropertyList_format_options_error_(plistDictionary, current application's NSPropertyListXMLFormat_v1_0, 0, missing value)
        
        if (plistData is not missing value) then
            plistData's writeToFile_atomically_(plistfile_path, true)
            else
            -- do error handling
        end if

Yep! Works now… Just changed {minSize:(minSize’s stringValue()), maxSize:(maxSize’s stringValue()), destinationFolderPOSIX:destinationFolderPOSIX, downloadsFolderPOSIX:downloadsFolderPOSIX}

Thanks a lot Stefan.

then I guess maxSizeValue and minSizeValue are not (global) properties nor instance variables.
That’s probably the reason for the failure of your script.
Each handler has its own scope and local variables can be used only within this scope

You are right, in my script minSize & maxSize are defined just as properties, to bind them to the relative textfield…
Well, i tried to create 2 extra variables before asking here, something like this:

set minSizeValue to minSize , but it didn’t work…

Honestly, I’m not a pro, i started to program in objective c one year ago with a trial&error approach, which is a good way to learn fast, but also full of headaches!

Applescript looks simple yet powerful, a lot more intuitive than objC in my opinion. I guess that learning it, can help with objC understanding as well.

For handler you mean this?

 
on doSomething()_   
 -- instructions.. 
end doSomething()_  

Yes, and also the script object itself
To pass local parameters you could write


on buttonExitClicked_(sender)
	set maxSizeValue to maxSize's stringValue()
	set minSizeValue to minSize's stringValue()
	makenewPlist(maxSizeValue, minSizeValue)
end buttonExitClicked_

on makenewPlist(max, min)
	
	-- .
	
	make new property list item at end of property list items of contents of this_plistfile ¬
	with properties {kind:string, name:"minSize", value:min}
	make new property list item at end of property list items of contents of this_plistfile ¬
	with properties {kind:string, name:"maxSize", value:max}
	
	-- .
		
end makenewPlist


yes …took me a while to understand, but got it… i have to pass variables in that way :smiley:

I don’t want to go off topic, but I’m trying to understand this kind of situations:

global val

on readKey(posixPath, keyname)
-- (cut)
return val
end readKey

readKey("/Users/Funder/Desktop/mp3cranefscript_settings.plist", "destinationFolderPOSIX")
set keyReturned to val
say val

error “val variable is not defined.” number -2753 from “val”

but it’s global!?

the variable is declared (~ registered), but not defined (no value assigned)

There is no need for a global variable in this case


on readKey(posixPath, keyname)
	set plistDict to current application's NSDictionary's dictionaryWithContentsOfFile_(posixPath)
	return (plistDict's objectForKey_(keyname)) as text
end readKey

set keyReturned to readKey("/Users/Funder/Desktop/mp3cranefscript_settings.plist", "destinationFolderPOSIX")
if keyReturned is "missing value" then
	say "an error occurred"
else
	say keyReturned
end if

I’ve changed the first part to this, now it’s working, but it’s the right way?

on readKey(posixPath, keyname)

	tell application "System Events"
		set p_list to property list file (posixPath)
		return (value of property list item keyname of p_list)
	end tell

end readKey

There are several “right” ways.
The syntax is correct, you could omit the parentheses in the System Events tell block