List directory with custom name applescript - applescript

I am looking for a way to display this list with a few custom tags.
Could anybody help me out? I only found this script and is very useful to me if I could alter a few parameters. However I have zero applescript experience.
I would like to have img, description and title as extra variable.
So the output would look like this: ( I will fill in title & description in later ofc. )
{
"title": "",
"gallery": [
{
"img": "101.jpg",
"title": "",
"description": ""
},
{
"img": "102.jpg",
"title": "",
"description": ""
}
]
}
etc for all images in that folder
Please find the script that outputs me my content of a chosen folder as an array. which outputs now as {"P1000443.JPG", "P1000444.JPG"}
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
end tell
Thanks!

What you're asking is complicated so let me get you started. Run this script, choose a file, and look at the results.
set aFile to choose file
tell application "Finder" to return properties of aFile
You will see all of the properties you can get from a file. Right now in your code you are asking only for the "name" property so you'll have to figure out what properties you want and ask for them in a similar manner as you have asked for the name. Then you'll have to convert all of that information into a nice organized list as you request... that's the difficult part. If you want something that you can't find in the properties then you'll have to figure out how to get that information in another way, for example "description".
EDIT: Based on your comment, I suggest you just use a repeat loop and create a string any way you want. Here's one example. You can use this technique to create the output in any format you want.
set file_list to {"P1000443.JPG", "P1000444.JPG"}
set outputString to ""
repeat with i from 1 to count of file_list
set outputString to outputString & "img: " & item i of file_list & return & "title: " & return & "description: " & return & return
end repeat
return outputString
The output based on the way I formatted it will look like this...
img: P1000443.JPG
title:
description:
img: P1000444.JPG
title:
description:
Good luck.

Related

How can I replicate programmatically in VBS what Word does when I insert a "built-in" property from the Insert->QuickPart->Document Property dropdown?

In Microsoft Word (Windows desktop version from around 2007 and later), I can use the Insert Tab, Text group, Explore Quick Parts dropdown, Document Property dropdown to insert a Content Control that allows display/entry/selection of the value of one of a number of types of Document Property, which might be one of the following:
Built-in document properties
"Content Type" metaproperties associated with a server such as Microsoft's SharePoint server
How can I do programmatic insertion of "built-in" document properties using VBScript?
The following example assumes that you want to insert a content control mapped to one of Word's built-in properties at a current valid selection in Windows Desktop Word (i.e. in an open document). Please see the following notes.
Option Explicit
' a simple test - assume a document is open in Word
' and you want to insert a "Property Content Control"
' at the current selection
Dim wapp
Set wapp = Getobject(,"Word.Application")
Call insertAndMapProperty(wapp.Selection.Range,"companyfax")
Set wapp = Nothing
Sub insertAndMapProperty(Location, PropertyName) ' As Word.Range, As String
' Location is a Word Range where you want to insert the Content Control
'
' pass the name of the element (since it does not change when you change the user interface language)
Select Case LCase(Trim(PropertyName))
Case "abstract"
setCoverPageProps Location, "Abstract", "Abstract"
Case "category"
setMSCoreProps Location, "category", "Category"
Case "company"
setExtendedProps Location, "Company", "Company"
Case "contentstatus"
setMSCoreProps Location, "contentStatus", "Status"
Case "creator"
setDCoreProps Location, "creator", "Author"
Case "companyaddress"
setCoverPageProps Location, "CompanyAddress", "Company Address"
Case "companyemail"
setCoverPageProps Location, "CompanyEmail", "Company E-mail"
Case "companyfax"
setCoverPageProps Location, "CompanyFax", "Company Fax"
Case "companyphone"
setCoverPageProps Location, "CompanyPhone", "Company Phone"
Case "description"
setDCoreProps Location, "description", "Comments"
Case "keywords"
setMSCoreProps Location, "keywords", "Keywords"
Case "manager"
setExtendedProps Location, "Manager", "Manager"
Case "publishdate"
setCoverPageProps Location, "PublishDate", "Publish Date"
Case "subject"
setDCoreProps Location, "subject", "Subject"
Case "title"
setDCoreProps Location, "title", "Title"
Case Else
Wscript.Echo "Unrecognized property name: " & PropertyName
End Select
End Sub
Sub setCoverPageProps(Location, PropertyName, TitlePlaceHolder)
Const missing = Nothing
Const coverPageMappings = "xmlns:ns0='http://schemas.microsoft.com/office/2006/coverPageProps'"
With Location.ContentControls.Add(1) '1=wdContentControlText
.Title = TitlePlaceHolder
.XMLMapping.SetMapping "/ns0:CoverPageProperties[1]/ns0:" & PropertyName & "[1]", coverPageMappings, missing
.SetPlaceHolderText missing, missing, "[" & TitlePlaceHolder & "]"
End With
End Sub
Sub setDCoreProps(Location, PropertyName, TitlePlaceHolder)
Const missing = Nothing
Const DCoreMappings = "xmlns:ns0='http://purl.org/dc/elements/1.1/' xmlns:ns1='http://schemas.openxmlformats.org/package/2006/metadata/core-properties'"
With Location.ContentControls.Add(1)
.Title = TitlePlaceHolder
.XMLMapping.SetMapping "/ns1:coreProperties[1]/ns0:" & PropertyName & "[1]", DCoreMappings, missing
.SetPlaceHolderText missing, missing, "[" & TitlePlaceHolder & "]"
End With
End Sub
Sub setMSCoreProps(Location, PropertyName, TitlePlaceHolder)
Const missing = Nothing
Const MSCoreMappings = "xmlns:ns0='http://schemas.openxmlformats.org/package/2006/metadata/core-properties'"
With Location.ContentControls.Add(1)
.Title = TitlePlaceHolder
.XMLMapping.SetMapping "/ns0:coreProperties[1]/ns0:" & PropertyName & "[1]", MSCoreMappings, missing
.SetPlaceHolderText missing, missing, "[" & TitlePlaceHolder & "]"
End With
End Sub
Sub setExtendedProps(Location, PropertyName, TitlePlaceHolder)
Const missing = Nothing
Const extendedMappings = "xmlns:ns0='http://schemas.openxmlformats.org/officeDocument/2006/extended-properties'"
With Location.ContentControls.Add(1)
.Title = TitlePlaceHolder
.XMLMapping.SetMapping "/ns0:Properties[1]/ns0:" & PropertyName & "[1]", extendedMappings, missing
.SetPlaceHolderText missing, missing, "[" & TitlePlaceHolder & "]"
End With
End Sub
Notes:-
By "valid selection", I mean a selection that won't cause a Word error/exception. I.e., you will need to do a lot more to avoid exceptions.
The descriptive (Title, Placeholder) texts here are for when you insert these controls with the Word User Interface Language set to English (and possibly only some specific English versions). If your interface language is, say, French, you might prefer to use the French equivalents for the Content Control Title and Placeholder. It is not obvious how you might discover those texts from the Word Object Model.
There are a number of different types of "property" in Word, including
builtin core properties
builtin app properties
builtin extended properties
builtin CoverPageProps properties
legacy user-defined properties (Word Custom Document Properties)
content-type properties
(arguably) Word Document Variables
programmer defined elements/attributes in Word Custom XML Parts.
You can insert the values of a lot of these properties using traditional Word "Field Codes" To insert values without using VBA (say), you need a Content Control mapped to a CustomXMLPart.
Within a .docx type document file (.docx, .docm, .dotx, .dotm), builtin core properties, builtin app properties, and builtin extended properties are stored in predefined .xml files. e.g. app.xml etc. However, when Word opens such a document, it builds two Word CustomXMLParts. Another standard custom XML part holds the values of the "CoverPageProps"
You can map a Content Control to any element or attribute within a Custom XML Part, including these parts. But Word does not replicate the values of builtin document properties such as "Number of Words", in CustomXML Parts, and that means that there is no builtin way to insert a Content Control that displays the number of Words in a document, or a Content Control that displays the value of a User-Defined Document Property.
Very great answer. For those interested to included other mapped properties, including those from SharePoint for instance, here is a StackOverflow post which gives further details on how to find the path to the XML properties: MS Word adding via VBA Custom Document Properties from SharePoint.
In brief:
Create an MS Word file in SharePoint library (with some user columns), and/or load custom XML properties in your file (see here)
Insert in the word file the control content manually to the properties you are interested in
Change word file extension to .zip
Extract [documentName.docx.zip]\word\document.xml document contained in the archive
Open the file with my favorite XML editor
You can then find the general path of the document at the following element\attributes: <w:dataBinding w:prefixMappings="[rootOfProperties]" w:xpath="[pathToProperties]" ....>
This information plugs into the code provided above.
NOTE: it might be possible to access the full details of the XML content as described in step 1-6, via the "Open XML SDK 2.0 Productivity Tool", as explained in this StackOverflow post: How to read metadata information from docx documents?

Logic not working with List items in Applescript

This is a very strange problem I could not understand it, the code is very clear as you can see, I don't know if I am tired or could not see something... please tell me why I am getting False as a result, while it should be True, I have a list with one item and it is the exact one in the variable
thanks
property forbidenFolders : {"/Volumes/USERS/"}
set ff to "/Volumes/USERS/" as text
my isForbidenFolder(ff)
on isForbidenFolder(SelectedFolder)
repeat with i in forbidenFolders
log "forbiden folders: " & i
log "actual folder : " & SelectedFolder
if i = SelectedFolder then
log "this folder is forbiden"
return true
end if
end repeat
log "NOT forbiden"
return false
end isForbidenFolder
result here
That's the reference trap.
The syntax repeat with item in list iterates thru the list with references e.g. a reference to item 1 of list, a reference to item 2 of list etc. rather than the item itself.
To be able to check for equality you have to dereference the item using contents of
if contents of i = SelectedFolder then
When i is set to the item of a list in a repeat loop, you are getting a reference to the item. You need to coerce it into a string for your comparison.
if (i as string) = SelectedFolder

Pass Multiple Applescript Variables to Ruby

I'm working on a Ruby script that requires some variables from Applescript. Right now I'm having success grabbing them one at a time, but it seems inelegant and possibly slow. For example:
note = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
if notebook of item 1 of the_selection is (notebook named "Writing") then
return HTML content of item 1 of the_selection
end if
else
return ""
end if
end tell
APPLESCRIPT}
title = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
if notebook of item 1 of the_selection is (notebook named "Writing") then
return title of item 1 of the_selection
end if
else
return ""
end if
end tell
APPLESCRIPT}
I know I'm probably overlooking something obvious, but is there an easy way I can do this with just a single snippet of Applescript that returns both variables into Ruby (note, title)?
Getting multiple values out of AppleScript is the easy part. Instead of returning a single value, return a list:
result = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
if notebook of item 1 of the_selection is (notebook named "Writing") then
set title to title of item 1 of the_selection
set html to HTML content of item 1 of the_selection
return { title, html }
end if
else
return ""
end if
end tell
APPLESCRIPT}
The hard part is parsing the output. Supposing your title is This is a note and html is <h1>Hello World</h1>, osascript will return this:
This is a note, <h1>Hello World</h1>
You could split it on the , but if title happens to contain a comma, you've got a problem. You can also pass the -ss option to osascript, which makes it return formatted AppleScript objects, but you don't want to have to write your own AppleScript parser in Ruby.
An alternative, if you know title will never contain a line break, is to put a line break after the title:
result = %x{ osascript <<APPLESCRIPT
...
if notebook of item 1 of the_selection is (notebook named "Writing") then
set title to title of item 1 of the_selection
set html to HTML content of item 1 of the_selection
return title & "\n" & html
end if
...
APPLESCRIPT}.chomp
Now the output (result) will look like this:
This is a note
<h1>Hello World</h1>
...and you can get the title and HTML like this:
title, html = result.split("\n", 2)
puts title
# => This is a note
puts html
# => <h1>Hello World</h1>
Now, if you have line breaks in any of your titles (I can't remember if Evernote allows that or not), or if you want to return more than two values, this will be problematic as well. The next simplest solution would be to choose some kind of delimiter that's unlikely to appear in any part of the output, for example %!%:
DELIMITER = '%!%'
result = %x{ osascript <<APPLESCRIPT
...
if notebook of item 1 of the_selection is (notebook named "Writing") then
set title to title of item 1 of the_selection
set html to HTML content of item 1 of the_selection
return title & "#{DELIMITER}" & html
end if
...
APPLESCRIPT}.chomp
# =>This is a note%!%<h1>Hello World</h1>
title, html = result.split(DELIMITER, 2)
If all else fails, you could use an addon to make osascript output a known format that Ruby knows how to parse. Just now I found this free JSON Helper that looks pretty handy.

Automator variable base on file name (Applescript?)

I am trying to use automator to rename files based on the folder I select as the input. I want to take the folder name and if 1 of 4 phrases are found in the folder name, 1 of 4 variables would be used. I don't know applescript but I feel this is the way to go base on other languages I know.
Can anyone convert the following concept?
if file name contains "USA" then
var = "US"
elseif file name contains "CAN_FR" then
var = "CAFR"
elseif file name contains "CAN_EN" then
var = "CAEN"
end
Much appreciation to anyone that can help.
Try:
on run {input, parameters}
tell application "Finder" to set folderName to name of first item of input
if folderName contains "USA" then
set var to "US"
else if folderName contains "CAN_FR" then
set var to "CAFR"
else if folderName contains "CAN_EN" then
set var to "CAEN"
else
set var to "Not Found"
end if
-- insert your code here
return var
end run

Applescript - Variable Commands Not Working When Using "Choose From List"

I'm trying to create a simple international store for iTunes.
set country to (choose from list {"US", "CA", "UK"} with prompt "What country?")
if country = "US" then
tell application "iTunes"
activate
open location "itmss://itunes.apple.com/WebObjects/MZStore.woa/wa/switchToStoreFront?storeFrontId=143441"
end tell
end if
When I click US, it doesn't do anything with iTunes. What am I doing wrong?
The problem is that the choose from list command is returning a list of the items selected from the list. You can do the following:
set country to (choose from list {"US", "CA", "UK"} with prompt "What country?")
if country = {"US"} then
tell application "iTunes"
activate
open location "itmss://itunes.apple.com/WebObjects/MZStore.woa/wa/switchToStoreFront?storeFrontId=143441"
end tell
end if
Alternatively, you could say if item 1 of country = "US".
Try
if country as text = "US" then

Resources