Background
I am working on an AppleScript application (ASOBJ) through Xcode and frequently access (update/retrieve) various entries from a .plist file that I have created.
Whenever I need to access the data, I use the following command:
set thePropertyListFilePath to POSIX path of (path to resource "Data.plist")
tell application "System Events"
tell property list file thePropertyListFilePath
set myAge to value of property list item "userAge" as text
end tell
end tell
The problem
My project is in development and file names / paths change all too frequently.
I want to make a property thePropertyListFilePath : [...] at the top of my project to be able to change the path and name of the file in one place, rather than multiple.
I would like to use property over set as it just works better in my case.
At the top of my code:
property thePropertyListFilePath : POSIX path of (path to resource "Data.plist")
Does not compile because:
Error: Resource not found.
Analysis
I know for a fact that the resource Data.plist is where it is supposed to be and the paths correct because it works fine from inside my top code snippet.
Leaving me with the question:
How do I set a property to a file path?
Any insight into why this error is thrown and how to correctly set the property to this (relative) file path is greatly appreciated. I am aware of this question, but it does not present an applicable solution. Cheers.
Edit
I now seem to be getting a related error:
System Events got an error: 'This
Mac:Users:myuser:Library:Developer:Xcode:DerivedData:ProjectName:Build:Products:Debug:ProjectName.app:Contents:Resources:Data.plist'
is not a property list file. (error -1728)
Some validation needs to take place before app has launched so I wrote my code in the applicationWillFinishLaunching_ section - this shouldn't present any problems.
I know that the file is there and a .plist format so I am thinking it could simply be the tell ... statements around it.
In the past, I used set currentVersion to (current application's class "NSBundle"'s mainBundle()'s objectForInfoDictionaryKey:"CFBundleShortVersionString") as text to access the version number from the Info.plist in one go. Is it possible to use a similar one-liner without the tell statements to retrieve userAge from my own plist file?
Don't do that. Never declare a property with a relative path. The value will set at compile time and will never change. That makes the benefit of the relative path useless.
Declare the property as empty string
property thePropertyListFilePath : ""
and set it in applicationDidFinishLaunching
on applicationDidFinishLaunching_(aNotification)
set thePropertyListFilePath to POSIX path of (path to resource "Data.plist")
end applicationWillFinishLaunching_
However as you are developing AppleScriptObjC I recommend the Cocoa way (longer but more efficient)
set thePropertyListFilePath to (current application's NSBundle's mainBundle()'s pathForResource_ofType_("Data", "plist")) as text
Related
I recently put together an AppleScript file which invloves reading a file at one point. The command I am using to do this is:
set paragraph_ores to read file "Macintosh HD:Users:MYUSERNAME:Desktop:ORES.txt" using delimiter linefeed
Now, whilst this works perfectly and as expected, the problem comes that I was hoping to integrate this into a separate application which would run this script when necessary but had the realisation that everybody who would need to use the code would have to go into this script and specifically put in their username which may be tricky or tedious for some people.
In an attempt to circumvent this problem I looked to Google where I found out some more information about HFS file paths to which I had no prior knowledge to find out that the general formula is:
<Volume Name>:<Directory Name>:...:<Directory Name>:<Filename>
And an example they give to use this type of file path is:
Macintosh HD:Applications:Safari.app
This proved confusing as it did not reference the username at all. Therefore I tried changing my original command line to:
set paragraph_ores to read file "Macintosh HD:Desktop:ORES.txt" using delimiter linefeed
However, this proved unsuccessful and I was presented with an error. My next thought was to set a variable to the current username and integrate this into the path as shown below:
tell application "System Events"
set username to name of current user
end tell
set paragraph_ores to read file "Macintosh HD:Users:" & username & ":Desktop:ORES.txt" using delimiter linefeed
Unfortunately, this too, for some reason, seemed not to work.
I would really appreciate it if someone could point me in the right direction of how to solve this problem of avoiding the username in the HFS file path.
Thank you in advance for your help,
Tom
Use the path to (folder) command, e.g.:
set paragraph_ores to read file ((path to desktop as string) & "ORES.txt")
Have a look at path to (folder) in the AppleScript Language Guide, which returns the location of the specified special folder.
I am trying to write an Apple Script for Sketch.app (com.bohemiancoding.sketch3). What i want to do is, create some image file that can be rendered in browser from Sketch document.
And when i open Sketch.app dictionary in Script Editior i see
saveable file format enum
Sketch : The native Sketch 2 file format
PDF : Portable Document Format
TIFF : Tagged Image File Format
So i thought about generating TIFF using following script, but it did not work
tell application "Sketch"
set curdoc to document 0
save curdoc in "/Users/mirza/Downloads/mew2" as TIFF
end tell
I can create sketch copies in .sketch format with save command but not PDF or TIFF. Does sketch supports PDF and TIFF using apple script?
Or is there any other way around for that.
Update
I change the path to apple script format and set document index to 1. Now script looks like this
set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
curdoc to document 1
save curdoc in thisFilePath as TIFF -- Tried with quotes as well, gives same error
end tell
But when i run the script i got the following error
Result:
error "Sketch got an error: Can’t continue curdoc." number -1708
Update 2
Fixed typo
set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
set curdoc to document 1
log (path of curdoc)
save curdoc in thisFilePath as "TIFF"
end tell
But when i run the script i got the following error
Result:
error "Sketch got an error: The document cannot be exported to the \"TIFF\" format." number -50
There are a number of things wrong with your code, but, to start with, you're going to find it hard to get definitive answers using software that isn't available anymore. Sketch has been at version 3 for a while now, and the AppleScript dictionary has probably changed.
That being said, here are some thoughts about your code:
If that is what the Sketch 2 AS dictionary reads, then the AS functionality has changed in v3.
I'd like to help, but I can't find v2 anywhere, so I can only do this in the dark.
set thisFilePath to choose file name--use this to select a new file;
------- a Mac AppleScript path is returned (a file specification,
------- actually, which is different from a string or alias
------- (but an alias is kind of like a file spec)
tell application "Sketch"
set curdoc to document 1--not zero-based; 1 is frontmost doc
save curdoc in thisFilePath as "TIFF"--*this is a guess
end tell
So, I don't know what that last save line will do, but it might work. In Sketch 3, "TIFF" format isn't allowed in saving, but it does have an as parameter as part of the save, which is supposed to be paired with a text string representing the format (like "TIFF", above). Sketch 2 seems to have a different scheme (the parameter with as is not a string). If I save without the as parameter in Sketch 3, it saves in Sketch's native format. So you might try this without quotes (like you have). I'm just doing what the v3 dictionary tells me to do.
Here are a couple of solutions and tips:
document 1 should work to reference the frontmost document;
If you want for some reason to write out your path using POSIX
(like you've done), you can use
POSIX file "/Users/mirza/Downloads/mew2"
to return an AppleScript's Mac-style path, which is of this form:
"yourHardDriveName:Users:mirza:Downloads:new2"
You can also get what I have here as "yourHardDriveHame:" by doing
tell application "Finder" to set sDr to startup disk as string
then concat by doing
sDr & "Users:mirza:Downloads:new2"
You can also do
tell application "Finder" to set myHome to home as string
which should return the Mac-style path to the home folder. (And yes, there are other paths Finder allows you to get, too).
There's some stuff to play with.
So, i'm working on a script that copies the home folder to a mounted shared drive folder. But i'm getting the following error:
"Handler can't handle objects of this class number -10010"
This is the code I came up with following the example of other code i've seen on here. I'm guessing that it is the way i'm telling finder to duplicate.
set vserver to ("/Volumes/sharedfolder")
set source to ("/Users/user")
tell application "Finder"
duplicate source to vserver
end tell
How else can I write this?
I've also tried running a boolean test to see if Finder saw the shared folder or my home folder and it retured false. (but only one false when it should have returned two)
tell application "Finder"
setaBoolean1 to get (exists vserver)
setaBoolean1 to get (exists source)
end tell
set vserver to ("/Volumes/sharedfolder")
The line above sets the variable vserver to a string object consisting of "/Volumes/sharedfolder". Likewise, the set source to "/Users/user" line sets source to a string object containing "/Users/user". Note that strings are not what the Finder is expecting when you're telling it to duplicate items.
The tell app Finder line is basically trying to tell the Finder to duplicate one string into another string, which it doesn't know how to do (hence the Handler can't handle objects of this class message).
What you need to do is to, instead of creating strings, create some sort of file system reference to those folders, so that the Finder knows how to deal with them.
There are numerous ways to do this, but the method I found that works (which uses the same POSIX style path format) is the following:
set vserver to POSIX file "/Volumes/sharedfolder"
set source to POSIX file "/Users/user"
tell application "Finder"
duplicate source to vserver
end tell
I'm starting to poke around with Applescript and am looking at writing a few scripts for managing windows. A common task they will all need is to get the current screen size.
I've created a screen_size subroutine that seems to work, and I want to be able to share that with all my scripts. However, I can't figure out a way to put that in a separate file that I can load in my other scripts. I tried creating a separate screen_size.scpt file and use load script "screen_size.scpt", but I get an error about "can't make "screen_size.scpt" into a type file".
There has to be a way to do this, but I haven't been able to find anything online about how to do it.
EDIT:
The POSIX stuff suggested isn't working for me. I'm able to create the file object, but it refuses to convert to an alias, saying it can't find the file (looks like the POSIX file stays relative instead of expanding fully).
I found a suggestion online to use Finder, and have gotten the following working to get an alias:
tell application "Finder"
set _myPath to container of (path to me) as text
end tell
set _loadPath to (_myPath & "screen_size.scpt")
set _loadAlias to alias _loadPath
However, the next line fails with a syntax error, claiming that _loadAlias isn't a variable:
property _ScreenSize : load script _loadAlias
Every variation of this I've tried (doing the alias in the load call, etc) fails, always claiming the variable doesn't exist, even though I know it's being set and working as I can display it. What's going on? Why is it claiming a variable doesn't exist when it obviously does?
AppleScript is doing some really weird things when saving and I haven't figured out what's going on, but I ended up getting something to work.
Here's what I have:
on load_script(_scriptName)
tell application "Finder"
set _myPath to container of (path to me) as text
end tell
set _loadPath to (_myPath & _scriptName)
load script (alias _loadPath)
end load_script
set _ScreenSize to load_script("screen_size.scpt")
set _bounds to _ScreenSize's screen_size()
-- ...
The other answers were saying to set _ScreenSize as a property, but that would cause a syntax error which prevented me from ever saving the file. When I did it just using set it worked.
I wasn't ever able to get the POSIX path stuff suggested to work, but poking Finder for the path worked fine.
In order to execute an action from another script, you'll have to create an handler in the script you're going to load (in your answer you already did this with "screen_size()".
In your case this script will be "screen_size.scpt".
So "screen_size.scpt" will have to look something like this:
on screen_size()
--your actions
return [yourvalue] --the value you want to pass to the other script
end screen_size()
The script you'll load it from will have to look like this:
tell application "Finder"
set _myPath to (container of (path to me) as text & "screen_size.scpt") as alias
end tell
set _ScreenSizeScript to load script _myPath
set _bounds to _ScreenSizeScript's screen_size()
If it doesn't work, or you don't understand me completely, feel free to ask (:
Yes there is a way to do this. Load the file into a property and access it that way
property _ScreenSize : load script (alias "pathtoscript")
_ScreenSize's doStuff()
and for relative paths try this:
set p to "./screen_size.scpt"
set a to POSIX file p
so perhaps this will work:
set p to "./screen_size.scpt"
set a to POSIX file p
property _ScreenSize : load script (alias a)
_ScreenSize's doStuff()
I have people using my libraries on a daily basis, so I first ensure the library is here before calling it.
Let's say I have a library "Lib.Excel.app" (save as non-editable application with Satimage's Smile).
At the beginning of a script that makes use of it, I "load" the library by using this code :
set commonCodeFile to (path to library folder as string) & "Scripts:CommonLibraries:Lib.Excel.app"
tell application "Finder"
if not (exists (file commonCodeFile)) then error ("\"Lib.Excel\"
" & "
should be found in folder
" & "
scroll > CommonLibraries")
end tell
global cc -- make it short and easy to write :)
set cc to load script alias ccFile
Then when I have to use a function from the lib, I just call it like this :
set {what, a} to cc's veryNiceFunction()
Yes you can. You need the full path to the script however.
I believe you can still use "path to me" to get the path to the app executing the current script, and you can then modify that path to point to your sub-folder containing the scripts.
I used this technique to get around AppleScripts (former) 32k text size limits years ago for some really large/complex IRC scripting.
I think I still have all those old scripts in my G4, which is under the desk in my office at work. Sadly it's behind a Enet switch and I can't VNC into it otherwise I'd have tons of sample code to post.
You CAN load the script in a variable, but you have to declare it first.
property _ScreenSize : missing value
tell application "Finder" to set _myPath to container of (path to me) as text
set _loadPath to (_myPath & "screen_size.scpt")
set _loadAlias to alias _loadPath
set _ScreenSize to (load script _loadAlias)
I'm trying to determine the path of track selected in iTunes using AppleScript. It doesn't seem to be a property of the track class. Can anyone tell me how I can get the file path?
Try this:
--gets file path of selected song
tell application "iTunes"
set songLocation to get location of selection
end tell
return songLocation
--gets file path of currently playing song
tell application "iTunes"
set songLocation to get location of current track
end tell
return songLocation
If it's not available through AppleScript, your best bet will be to open and parse the iTunes plist file. If you only need static information, you're golden. If you need dynamic information (for example, info on the track that's currently playing), you'll want to get the track's persistent ID through AppleScript, then parse the plist file and lookup any info you need (including location, which has the full path).
For parsing that plist XML file (in ~/Music/iTunes/iTunes Music Library.xml), you can use ruby or python or any other language you like. If you like python, try this library:
http://docs.python.org/library/plistlib.html
If you're going to use Python instead of AppleScript and don't want to parse the plist XML file, you can get the file path through the COM API.
import win32com.client
iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
currentTrack = win32com.client.CastTo(iTunes.CurrentTrack,"IITFileOrCDTrack")
print currentTrack.Location