Wolfram Research

Function Repository Resource:

ObjectExistsQ

Source Notebook

Check if an object exists

Contributed by: Bob Sandheinrich

ResourceFunction["ObjectExistsQ"][obj]

gives True if the object exists and False otherwise.

ResourceFunction["ObjectExistsQ"]["Register",head,test]

registers test as the function to call for objects of Head head in ResourceFunction["ObjectExistsQ"].

Details and Options

URLs are considered to exist if an http request gives a 2XX or 3XX status code.
Tasks are considered to exist if the have not completed.
ResourceFunction["ObjectExistsQ"] returns False for unknown object types.
More object types can be added for the kernel session using ResourceFunction["ObjectExistsQ"]["Register",].

Examples

Basic Examples

Check if a PermissionsGroup exists:

In[1]:=
ResourceFunction["ObjectExistsQ"][PermissionsGroup["bobsbesties"]]
Out[1]=

Create the group:

In[2]:=
CreatePermissionsGroup["bobsbesties"];

Now it exists:

In[3]:=
ResourceFunction["ObjectExistsQ"][PermissionsGroup["bobsbesties"]]
Out[3]=

Scope

BoxObject and CellObject

Create a BoxObject and CellObject:

In[4]:=
cell = ResourceFunction[
   "PrintAsCellObject"][{ResourceFunction["WolfieSay"][
    "Are you my bestie?"], DynamicWrapper[ResourceFunction["BirdSay"]["Umm....."], box = EvaluationBox[]]}]
Out[4]=

Both exist and can be deleted with NotebookDelete:

In[5]:=
ResourceFunction["ObjectExistsQ"] /@ {box, cell}
Out[5]=
In[6]:=
NotebookDelete@box
In[7]:=
ResourceFunction["ObjectExistsQ"] /@ {box, cell}
Out[7]=
In[8]:=
NotebookDelete@cell
In[9]:=
ResourceFunction["ObjectExistsQ"] /@ {box, cell}
Out[9]=

ChannelObject

Using ChannelObject does not actually create a channel:

In[10]:=
ResourceFunction["ObjectExistsQ"][ChannelObject["BestiesChatChannel"]]
Out[10]=

Using CreateChannel makes the channel live:

In[11]:=
ResourceFunction["ObjectExistsQ"][ChannelObject["BestiesChatChannel"]]
Out[11]=

CloudObject

Using CloudObject does not actually create a cloud object:

In[12]:=
ResourceFunction["ObjectExistsQ"][CloudObject["BestiesInfo"]]
Out[12]=

Using CloudPut or similar functions creates the object:

In[13]:=
CloudPut[<|
   "Joel" -> <|"Sweet WL Coder" -> True, "Baseball Fan" -> True, "Professional Wrester" -> False|>|>, "BestiesInfo"];
In[14]:=
ResourceFunction["ObjectExistsQ"][CloudObject["BestiesInfo"]]
Out[14]=

Databin

Check if a Databin exists:

In[15]:=
ResourceFunction["ObjectExistsQ"][
Databin["I5b8CGbd"]]
Out[15]=

DatabaseReference

A DatabaseReference exists if it is backed by a file that exists or if it is connected:

In[16]:=
ResourceFunction["ObjectExistsQ"][
 DatabaseReference[FindFile["ExampleData/ecommerce-database.sqlite"]]]
Out[16]=
In[17]:=
ResourceFunction["ObjectExistsQ"][
 DatabaseReference["bobsbesties.sqlite"]]
Out[17]=

ExternalSessionObject

External sessions exist while they are active:

In[18]:=
session = StartExternalSession["Python"]
Out[18]=
In[19]:=
ResourceFunction["ObjectExistsQ"][session]
Out[19]=

Use DeleteObject to end the session:

In[20]:=
DeleteObject[session]
In[21]:=
ResourceFunction["ObjectExistsQ"][session]
Out[21]=

File

For File, ObjectExistsQ is equivalent to FileExistsQ:

In[22]:=
ResourceFunction["ObjectExistsQ"][File[$InstallationDirectory]]
Out[22]=
In[23]:=
ResourceFunction["ObjectExistsQ"][File["/besties/former/shunned"]]
Out[23]=

LocalObject

For LocalObject, ObjectExistsQ is equivalent to FileExistsQ:

In[24]:=
ResourceFunction["ObjectExistsQ"][LocalObject["BestiesPhoto"]]
Out[24]=

Use Put to create the local object:

In[25]:=
Put[ResourceFunction["FirstWebImage"]["Friends"], LocalObject["BestiesPhoto"]];
In[26]:=
ResourceFunction["ObjectExistsQ"][LocalObject["BestiesPhoto"]]
Out[26]=

NotebookObject

A NotebookObject exists only if the window has not been closed:

In[27]:=
nb = CreateDocument[TextCell["An Essay About My Besties", "Title"]]
Out[27]=
In[28]:=
ResourceFunction["ObjectExistsQ"][nb]
Out[28]=

Closing the window destroys the NotebookObject:

In[29]:=
NotebookClose[nb]
In[30]:=
ResourceFunction["ObjectExistsQ"][nb]
Out[30]=

PermissionsGroup

Check if a PermissionsGroup exists:

In[31]:=
ResourceFunction["ObjectExistsQ"][PermissionsGroup["bobsbesties"]]
Out[31]=

Create the group:

In[32]:=
CreatePermissionsGroup["bobsbesties"];

Now it exists:

In[33]:=
ResourceFunction["ObjectExistsQ"][PermissionsGroup["bobsbesties"]]
Out[33]=

PersistentObject

Using PersistentObject does not actually create a persistent object:

In[34]:=
po = PersistentObject["MyBestBestie", "KernelSession"]
Out[34]=
In[35]:=
ResourceFunction["ObjectExistsQ"][po]
Out[35]=

Assign a value to create the object:

In[36]:=
po["Value"] = "Joel"
Out[36]=
In[37]:=
ResourceFunction["ObjectExistsQ"][po]
Out[37]=

ResourceFunction

Check if a ResourceFunction exists:

In[38]:=
rf = DefineResourceFunction[AppendTo[$BobsBesties, #] &, "AddToBesties"]
Out[38]=
In[39]:=
ResourceFunction["ObjectExistsQ"][rf]
Out[39]=

Delete the resource:

In[40]:=
DeleteObject[rf]
In[41]:=
ResourceFunction["ObjectExistsQ"][rf]
Out[41]=

Functions published in the repository always exist:

In[42]:=
ResourceFunction["ObjectExistsQ"][ResourceFunction["BirdSay"]]
Out[42]=
In[43]:=
ResourceFunction["BirdSay"]["I ain't migratin' nowhere"]
Out[43]=

ResourceObject

Check if a ResourceObject exists:

In[44]:=
ResourceFunction["ObjectExistsQ"][ResourceObject["Bob"]]
Out[44]=

SearchIndexObject

Check if a SearchIndexObject exists:

In[45]:=
index = CreateSearchIndex["ExampleData/Text"]
Out[45]=
In[46]:=
ResourceFunction["ObjectExistsQ"][index]
Out[46]=

Delete the index:

In[47]:=
DeleteSearchIndex[index]
In[48]:=
ResourceFunction["ObjectExistsQ"][index]
Out[48]=

TaskObject

A task only exists until it is complete:

In[49]:=
task = SessionSubmit[
  ScheduledTask[Speak["Bob Loves His Besties"], {3}]]
Out[49]=
In[50]:=
ResourceFunction["ObjectExistsQ"][task]
Out[50]=
In[51]:=
Pause[5]; ResourceFunction["ObjectExistsQ"][task]
Out[51]=

URL

See if a URL exists:

In[52]:=
ResourceFunction["ObjectExistsQ"][
 URL["https://www.twitter.com/wolframtap"]]
Out[52]=
In[53]:=
ResourceFunction["ObjectExistsQ"][
 URL["https://www.twitter.com/bobsbesties"]]
Out[53]=

Streams

Test if an InputStream or OutputStream is open:

In[54]:=
Put["BobsBesties"]
In[55]:=
stream = OpenRead["BobsBesties"]
Out[55]=
In[56]:=
ResourceFunction["ObjectExistsQ"][stream]
Out[56]=

Close the stream:

In[57]:=
Close[stream]
Out[57]=
In[58]:=
ResourceFunction["ObjectExistsQ"][stream]
Out[58]=

Strings

Strings are either interpreted as URLs or files:

In[59]:=
ResourceFunction["ObjectExistsQ"]["http://www.wikipedia.org"]
Out[59]=
In[60]:=
ResourceFunction["ObjectExistsQ"][$InstallationDirectory]
Out[60]=
In[61]:=
ResourceFunction[
 "ObjectExistsQ"]["this string is treated as a file path"]
Out[61]=

Properties and Relations

Unknown object types give False:

In[62]:=
ResourceFunction["ObjectExistsQ"][BestieObject["Joel"]]
Out[62]=

Register a new object type:

In[63]:=
ResourceFunction["ObjectExistsQ"]["Register", BestieObject, MemberQ[$MyBesties, First[#]] &]
Out[63]=

Now the specified function is used on the object type to determine whether it exists:

In[64]:=
$MyBesties = {"Joel"};
In[65]:=
ResourceFunction["ObjectExistsQ"][BestieObject["Joel"]]
Out[65]=
In[66]:=
ResourceFunction["ObjectExistsQ"][BestieObject["Joseph"]]
Out[66]=

Resource History

Related Resources

License Information