How can the original target path be programatically retrieved when the alias fails to resolve?
do {
let resolutionOptions: URL.BookmarkResolutionOptions = [
.withoutUI, .withoutMounting
]
let _ = try URL(resolvingAliasFileAt: fileURL, options: resolutionOptions)
}
catch _ {
// since non-resolvable, then retrieve & print original target string
}
The existing StackOverflow question "Getting alias path of file in swift" does not cover original target path retrieval for the situation of a non-resolvable alias.
The information would seem to be available somehow because the Finder GUI Get Info will still show the Original: /Some/Path even if the original is not found or available.
Also, mdls metadata listing did not provide the original target path.
I think you can load the bookmark data using URL.bookmarkData(withContentsOf:), then use resourceValues(forKeys:fromBookmarkData:) with [.pathKey] as the keys. Then, query the path of the returned URLResourcesKey object.
Related
Given a list of repositories on GitHub with 'repoName' and 'userName', I need to find all the '.java' files, and get the content of the java files. Tried to use RestAPI first, but ran into the rate limit very easily, now I'm switching to GraphQL API, but don't know how to achieve that.
Here is how I would do it:
Algo Identify_java_files
Entry: path the folder
Out: java files in the folder
Identify all files in the folder of the repository
For each file
if the type of the file is "blob":
if ".java" is the end of the name of the file
get its content
else if the type of the file is "tree":
Identify_java_files(path of the tree file)
You can easily implement this pseudo code using Python. My pseudo code makes the assumption to use recursion, but it can be done otherwise, it's just for the example. You will need the requests and json libraries.
Here are the queries you might need, and you can use the explorer to test them.
{
repository(name: "checkout", owner: "actions") {
defaultBranchRef {
name
}
}
}
This query allows you to check the name of the default branch of the repository. You will need it for the following queries, or you can use a specific branch but you will have to know its name. I don't know (and don't believe) if you can get all the branches names for a repository.
{
repository(name: "checkout", owner: "actions") {
object(expression: "main:") {
... on Tree {
entries {
path
type
}
}
}
}
}
This query gets the content of the root folder for a specific repository. The expression: "main:" refers to the branch of the repository along with the path. Here the branch is main and the path is empty (it comes after the ":"), meaning we are looking at the root folder. Some repositories are using "master" as default branch, so be sure of which branch to use.
To check the content of a file, you can use this accepted answer.
I updated the example in order for you to be able to try it.
{
repository(name: "checkout", owner: "actions") {
object(expression: "main:.github/workflows/test.yml") {
... on Blob {
text
}
}
}
}
You send your requests to the API using requests or alike, and store the responses as JSON or alike for treatment.
As a side note, I do not think it is possible to achieve this without issuing multiple queries. I recently had to do something similar, and this is my first SO answer, so let me know if anything is unclear.
Edit:
You can use this answer to list all files in a repository.
I need a way of checking when a file was last opened. I tried by creating a custom FileAttributeKey and setting that to the current Date, but when I go to open the file again the attribute does not exist:
private let key = FileAttributeKey(rawValue: "lastOpenedAt")
do {
try FileManager.default.setAttributes(
[key: Date()],
ofItemAtPath: videoNameDirectoryPath
)
} catch {
Log.error(error.localizedDescription)
}
So now I am resorting to using the modification date key to say when I last opened the file, it is not ideal so I am wondering if there is a better way to do this
setAttributes doesn't support custom attributes, you can only use the documented ones.
To set your own attributes, you may use xattr as described in this question:
Write extend file attributes swift example
If you're lucky, you may use kMDItemLastUsedDate from Spotlight aka MDItem as described in the documentation archive of File Metadata Attributes.
I was looking at the documentation for contentsOfDirectory(at:includingPropertiesForKeys:options:)
Particularly I've been focusing on the argument includingPropertiesForKeys, which said:
An array of keys that identify the file properties that you want pre-fetched for each item in the directory. For each returned URL, the specified properties are fetched and cached in the NSURL object. For a list of keys you can specify, see Common File System Resource Keys.
Clicking on URLResourceKey led me to the Apple Documentation about it.
And I was wondering, if I passed in keys like fileResourceTypeKey, fileResourceIdentifierKey, and creationDateKey how could I access those in the returned URL list (after calling contentsOfDirectory(at:includingPropertiesForKeys:options:))?
And I was also confused by the URLResourceKey enum b/c a lot of types have similar descriptions and names to other keys like:
documentIdentifierKey vs fileResourceIdentifierKeyvs localizedNameKey vs nameKey
localizedTypeDescriptionKey vs fileResourceTypeKey vs typeIdentifierKey
the urls returned by the contentsOfDirectory(at:includingPropertiesForKeys:options:) vs pathKey
Like what would be the differences between these keys?
Basically I have a really low understanding of the file system at this point so please bear with my "simple" questions. If someone could explain what all these keys means and how I can access/use them that would be great!
First of all the URLResourceKey documentation describes the kind of the attribute information very well. For example nameKey returns always Desktop for an URL representing ~/Desktop while localizedNameKey returns the localized name Schreibtisch on a German system or Bureau on a French system. However documentIdentifierKey and fileResourceIdentifierKey are completely different attributes.
Regarding the contentsOfDirectory(at:includingPropertiesForKeys:options:) API: The keys passed in the includingPropertiesForKeys parameter tells the framework to pre-fetch the corresponding attributes while getting the contents for performance reasons. For example
let contentURLs = try fileManager.contentsOfDirectory(at: anURL, includingPropertiesForKeys: [.nameKey, .fileSizeKey], options: .skipsHiddenFiles)
To read the attributes call resourceValues(forKeys on the URL passing the same keys as in contentsOfDirectory. Then get the value with the corresponding property of the resource key. The benefit of the combination URLResourceKey / URLResourceValues is you get always the proper type from the file attributes. This avoids any type casting.
for fileURL in contentURLs {
do {
let fileAttributes = try fileURL.resourceValues(forKeys:[.nameKey, .fileSizeKey])
print(fileAttributes.name!) // is String
print(fileAttributes.fileSize!) // is Int
} catch { print(error, fileURL) }
}
Is it possible for a visual studio code extension to make a file-path use a specific language, like files.associations.
This is to associate a json schema with a specific unusual json file, with its schema. The schema association is working fine, but only if I manually set the file grammar to json. Is there any way to do this automatically with the extension (not for example by adding an association in user settings).
Edit: 6th October
Still unresolved, cannot see an official way to do this, however, I have got it working by doing:
let config = vscode.workspace.getConfiguration()
if (config.get("files.associations")["*.mcmeta"] == undefined && !context.globalState.get("mcmeta- updated")) {
let object = config.get("files.associations");
object["*.mcmeta"] = "json";
config.update("files.associations", object, true);
vscode.window.showInformationMessage("...");
}
context.globalState.update("mcmeta-updated", true);
Which is essentially a massive hack to update the files.association property in the global settings
I have a file reader channel picking up an xml document. By default, a file reader channel populates the 'originalFilename' in the channel map, which ony gives me the name of the file, not the full path. Is there any way to get the full path, withouth having to hard code something?
You can get any of the Source reader properties like this:
var sourceFolder = Packages.com.mirth.connect.server.controllers.ChannelController.getInstance().getDeployedChannelById(channelId).getSourceConnector().getProperties().getProperty('host');
I put it up in the Mirth forums with a list of the other properties you can access
http://www.mirthcorp.com/community/forums/showthread.php?t=2210
You could put the directory in a channel deploy script:
globalChannelMap.put("pickupDirectory", "/Mirth/inbox");
then use that map in both your source connector:
${pickupDirectory}
and in another channel script:
function getFileLastModified(fileName) {
var directory = globalChannelMap.get("pickupDirectory").toString();
var fullPath = directory + "/" + fileName;
var file = Packages.java.io.File(fullPath);
var formatter = new Packages.java.text.SimpleDateFormat("yyyyMMddhhmmss");
formatter.setTimeZone(Packages.java.util.TimeZone.getTimeZone("UTC"));
return formatter.format(file.lastModified());
};
Unfortunately, there is no variable or method for retrieving the file's full path. Of course, you probably already know the path, since you would have had to provide it in the Directory field. I experimented with using the preprocessor to store the path in a channel variable, but the Directory field is unable to reference variables. Thus, you're stuck having to hard code the full path everywhere you need it.